# Object-Oriented Programming: Class vs Object

Object-oriented programming (OOP) is a programming model that uses the approach of **classes** and **objects** in designing or developing an application.

The main goal of Object-Oriented Programming (OOP) is to organize code in a way that reflects real-world entities and interactions, making applications more robust and easier to develop.

### **Class vs Object**

**Class**

A class is a *blueprint* or *template* that contains data and behavior *(method/function)* that can be accessed by creating an *instance* (object) of that class. For more clarity, let's look at the following image:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730098454014/ccdbd71e-d5f9-4f1e-9a73-f92d2ead896d.jpeg align="center")

The image above is an illustration of a *blueprint* or *template* of a car. The image is not the actual physical car, but just a sketch, which we call the car *class*.

The "Car" class has attributes such as color, brand, and model, as well as methods or functions like move and stop.

**Object**

An object is an *instance* or realization of a *class*, an *object* is like the physical form of a *class*. Objects can have their own data values that differ from one object to another. For more clarity, let's look at the following image.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730098719960/8cf814d8-a8eb-4262-a9b7-968319b11063.jpeg align="center")

The image above is no longer a *blueprint* or *template*, but has become a car object, which is called an *object*.

Now we can assign values to the car object's attributes according to the list of attributes specified in its *blueprint* or *class* (color, brand, and model), for example:

* Color = Blue.
    
* Brand = BMW.
    
* Model = i5 (just an example, I don't know what type of BMW the image is XD).
    

We can also execute the behavior or methods of the car (called *method/function*) such as the "move" and "stop" methods. When we execute its *method/function*, the car will perform the "move" or "stop" command.

### **Code Example**

Here is a code example for *class* and *object* in the C# programming language.

Example code for *class*:

```csharp
public class Car
{
    // This is an AutoProperty (C# 3.0 and higher)
    // used to generate a private field for you
    public string Color { get; set; }
    public string Brand { get; set; }
    public string Model { get; set; }

    public void Drive()
    {
        Console.WriteLine("The car is driving.");
    }
}
```

Example code for *object*:

```csharp
// This is an object of the Car class.
Car myCar = new Car();
myCar.Color = "Blue";
myCar.Brand = "BMW";
myCar.Model = "i5";
myCar.Drive();
```

Here is the difference between *class* and *object* in a table:

| **Aspect** | **Class** | **Object** |
| --- | --- | --- |
| Definition | Blueprint or template for creating objects. | An instance or realization of a class. |
| Memory | Does not consume memory until the class is initialized. | Consumes memory when the object is created. |
| Attributes | Defines the attributes (field/property) of the object. | Has values associated with its attributes. |
| Behavior | Defines the behavior (method) of the object. | Can call the behavior (method) defined by its class. |
| Usage | Used to create objects. | Used to perform operations based on the class blueprint. |

We have learned more about classes and objects, next we will look at the **4 basic principles of Object Oriented Programming (OOP)**, starting with the first principle, [**Inheritance**](https://kristiadhy.hashnode.dev/inheritence).
