1. Introduction

In this tutorial, we’ll talk about the differences between two basic object-oriented programming concepts: objects and classes.

2. Object Oriented Programming

Object Oriented Programming or OOP is a computer programming model that focuses on “what” rather than “how”. In it, we organize our code around data as opposed to other paradigms, such as functional programming, in which we base our design on operations we can do with input.

The building blocks of an OOP-based design are classes, objects, attributes, and methods:

OOPS Building Blocks

3. Class vs. Object

An attribute is a standalone part of the data an object has. A method is an operation we can do with an object. But, what are classes and objects, and what are their differences?

3.1. Role

A class differs from an object in terms of its role in the overall design.

So, a class is a template that we use to create as many objects as we need in our program. It defines the objects’ relevant properties (attributes) and behavior (methods). An object of a class is each entity that conforms to the class specifies and has all the attributes and methods it defines.

Let’s go through an example.

3.2. Example

Let’s suppose we model a human as a class we’ll name Person. We can define the following four attributes each person will have in our application:

  1. name
  2. age
  3. job_status
  4. gender

Of course, we can add other attributes such as surname, height, or weight. But, the point is that a class should define only those attributes that are relevant to our software. So, if we code a program that will need to know only the above four things about each person, we should define only them as the class’s attributes.

Next, we define two methods to model what a person can do with those attributes:

  1. GetNewJob()
  2. LeaveCurrentJob()

We’ll call the GetNewJob() method if a person gets a new job. It will change the attribute job_status from unemployed to employed. Similarly, to change the status back to unemployed, we’ll call the LeaveCurrentJob() method.

Schematically, this is our class:


Now, let’s create two objects of this class:


As we see, their attributes have specific values at the time of their creation.

To conclude, we call a class a user-defined type, whereas we can think of an object of that class as a variable of the type.

3.3. Definition vs. Instantiation

We can say that a class is never created and is only defined. That means that a class is an abstract model of real-world entities, so it can only have a definition. It can’t exist among its objects and can only describe them. We need to define classes fully before compilation/execution.

In contrast, objects model real-world entities. Consequently, we say they can be created, which is the process we call instantiation. This happens at the run time.

3.4. Attributes

To create an object, we need to assign specific values to the attributes its class specifies it should have.

In contrast, to define a class, we need to list all the attributes and methods its objects should have. For each attribute, we need to specify its name and type. In most programming languages, we also qualify each attribute with various access modifiers, such as private or public in Java. Those modifiers determine if attributes and methods are visible from outside the class and its objects.

3.5. Inheritance

The class can inherit each other. A child class incorporates the definition of its parent class. So, the attributes and methods of a parent are also present in their children. This comes with a condition that the parent’s attributes and methods are inheritable. For example, private methods in Java aren’t visible in child classes.

On the other hand, we don’t say objects inherit one another. Instead, objects’ types are the classes they belong to. If we instantiate an object x as an instance of class A and let B be the parent of A, we say that x is of types A and B. Since A is more specific, we declare x as a variable of type A, but it isn’t wrong to consider and use it as an object of type B as well. We can this polymorphism.

3.6. Changeability

We can’t change a class after defining it. This is how OOP works in most such languages.

On the other hand, we can alter objects after instantiation. That means we can change their attributes. The condition for this is that the definition of their class allows us to do it.

From this perspective, classes are static, and objects are dynamic constructs since the former can’t but the latter can change during the execution of our code.

3.7. Memory

Once we create an object, the operating system allocates memory for it. Objects are usually in the heap section of the process memory layout.

The definition of a class also takes a small amount of memory, but the actual management depends on the programming language. In C++, we store class definitions in the process code section. In Java, a class is treated the same way as an object, so it also lives in the heap.

4. Difference Summary

Let’s quickly summarize the difference between a class and an object. The following table highlights the key differences between the two:

Rendered by QuickLaTeX.com

5. Conclusion

In this article, we have revisited object-oriented programming and its two basic constructs: classes and objects.

A class is a formal description of a group of entities falling under a common definition and having common attributes and methods. It acts as a programming template for those entities, which we call objects. A class defines what an object is, what attributes it will have, and how it will behave. As a result, objects are instances of a class and always conform to the template of their class models.

Comments are closed on this article!