At the heart of OOP lie two fundamental concepts: classes and objects. They form the foundation of Object-Oriented Programming (OOP).
A class is a blueprint or template. It defines the properties (attributes) and behaviors (methods) that the objects created from it will have.
Think of a class as a recipe. It tells you what ingredients (fields) and instructions (methods) are needed, but it’s not the actual dish. You use the recipe to bake a cake (i.e., create an object).
This Car
class defines what every car object should look like and what it can do.
An object is an instance of a class. When you create an object, you are bringing the blueprint of the class into reality. It consists of state and behavior defined by the class, with each object holding its own copy of the data.
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.