Classes and Objects
The building blocks of OOP â a blueprint and its real instances.
What is a Class?
A class is a blueprint that defines properties (attributes) and behaviors (methods) that its objects will have. It doesn't hold real data itself â it's the template.
What is an Object?
An object is a specific instance of a class, created using that blueprint, with its own actual values for the class's attributes.
class Car {
constructor(brand) { this.brand = brand; }
drive() { console.log(this.brand + " is driving"); }
}
const myCar = new Car("Tata"); // object
myCar.drive();Why OOP?
OOP models real-world entities naturally, making large codebases easier to organise, reuse, and maintain compared to writing everything as separate functions.
đ Real-World Use
In a food delivery app, 'Order', 'Restaurant', and 'DeliveryPartner' would each be classes. Every actual order placed by a customer is an object created from the 'Order' class blueprint.
đĄ Pro Tip
A common mistake is confusing a class with an object in interview answers â always say 'a class is the blueprint, an object is the actual thing built from it' to show clear understanding.
đ§Ē Quick Self-Test
Check what you just learned â no pressure, just practice.
1. What is a class?
2. How many objects can you create from one class?