At the heart of OOP lie two fundamental concepts: classes and objects.
They are the foundation on which every OOP-based language like Java, Python, C++, C#, or TypeScript is built.
A class is a blueprint, template, or recipe for creating objects. It defines what an object will contain (its data) and what it will be able to do (its behavior).
A class is not an object itself, it’s a template used to create many objects with similar structure but independent state.
Think of a class like a recipe for a cake:
The recipe itself doesn’t produce a cake, it just defines how to make one. When you follow the recipe and bake a cake, you’ve just created an object.
Let’s define a simple Car class with essential attributes and methods that any Car object will have.
The following diagram and code show the blueprint for a Car:
This Car class defines what every car object should look like and what it can do.
An object is an instance of a class. It’s a real-world manifestation of the class blueprint, something you can interact with, store data in, and invoke methods on.
When you create an object, you’re essentially saying:
“Take this blueprint (class) and build one actual thing (object) out of it.”
Each object:
Let’s now create a few car objects using our Car class.
Here, corolla and mustang are objects of the Car class. They have their own brand , model , and speed fields and can use methods defined in the class.
Classes and objects are powerful when you need to model complex structures or real-world entities. But what if you simply want to define a fixed set of constants, values that rarely change and for which you only ever need one instance of each?
That's where Enums come in. Lets explore them next.