Skip to main content

Command Palette

Search for a command to run...

Object-Oriented Programming: Class vs Object

Class and Object: The fundamental concepts underlying Object Oriented Programming (OOP).

Updated
3 min read
Object-Oriented Programming: Class vs Object
K

Results-driven Full Stack .NET Developer specializing in building robust, maintainable enterprise applications. I leverage ASP.NET Core Web API and modern front-end frameworks like Blazor and React to deliver high-quality solutions

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:

AspectClassObject
DefinitionBlueprint or template for creating objects.An instance or realization of a class.
MemoryDoes not consume memory until the class is initialized.Consumes memory when the object is created.
AttributesDefines the attributes (field/property) of the object.Has values associated with its attributes.
BehaviorDefines the behavior (method) of the object.Can call the behavior (method) defined by its class.
UsageUsed 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.

More from this blog

Kris on code

18 posts

This is a blog where I share my journey as a developer—diving deep into the world of .NET, C#, ASP.NET Core, Blazor, and modern software practices.