AlgoMaster Logo

Classes and Objects

Ashish

Ashish Pratap Singh

At the heart of OOP lie two fundamental concepts: classes and objects. They form the foundation of Object-Oriented Programming (OOP).

1. What is a Class?

A class is a blueprint or template. It defines the properties (attributes) and behaviors (methods) that the objects created from it will have.

Key Characteristics

  • It encapsulates data and behavior.
  • Defines attributes as variables.
  • Defines methods (functions inside a class) to operate on that data.

Example: Class Blueprint

This Car class defines what every car object should look like and what it can do.

2. What is an Object?

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.

Creating Objects

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.