Object-Oriented Programming: Class vs Object

Table of contents

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:
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.
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:
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:
// 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.
Subscribe to my newsletter
Read articles from Kristiadhy directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Kristiadhy
Kristiadhy
Experienced Full Stack .NET developer with a proven track record of designing and implementing robust business applications. Proficient in using ASP.NET Core Web API, Blazor, and WinForms to deliver high-quality, efficient code and scalable solutions. Strong focus on implementing industry best practices for cleaner and scalable outcomes.