Sobes.tech
Junior — Middle

What is the difference between a class and an instance of a class in object-oriented programming?

sobes.tech AI

Answer from AI

A class is a template or description of an object that defines its properties (fields) and behavior (methods). It serves as a blueprint for creating objects.

An instance of a class (object) is a specific object created based on this template with its own field values.

Example in Java:

class Car {
    String color;
    void drive() {
        System.out.println("The car is driving");
    }
}

Car myCar = new Car(); // myCar is an instance of class Car
myCar.color = "Red";
myCar.drive();

Here, Car is the class, and myCar is an instance of that class.