Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) in JavaScript
Modern software applications are complex. To manage this complexity, developers use Object-Oriented Programming (OOP).
OOP helps us organize code in a way that represents real-world objects like students, cars, bank accounts, and users.
Instead of writing many unrelated functions, we create classes and objects that group related data and behavior together.
In this guide we will learn:
What Object-Oriented Programming means
Real-world analogy (Blueprint → Objects)
What is a class in JavaScript
Creating objects from classes
Constructor method
Methods inside a class
Encapsulation (basic idea)
Practical examples (Person, Car, Student)
Interview questions and answers
1.What is Object-Oriented Programming (OOP)?
Object-Oriented Programming is a programming style where we organize code using objects.
An object represents something from the real world and contains:
1️⃣ Properties (Data)
2️⃣ Methods (Actions / Functions)
Example: Car
A car has:
Properties (data)
brand
color
speed
Actions (behavior)
start()
stop()
accelerate()
In OOP we represent this using classes and objects.
Real-World Analogy: Blueprint → Object
The easiest way to understand OOP is through the blueprint concept.
Blueprint Example
When engineers design a house, they create a blueprint.
From one blueprint, many houses can be built.
In programming:
| Real World | Programming |
|---|---|
| Blueprint | Class |
| House | Object |
Diagram
Blueprint (Class)
↓
House1 House2 House3
Object Object Object
So remember:
Class = Blueprint
Object = Real instance created from blueprint
2.What is a Class in JavaScript?
A class is a template used to create objects.
It defines:
properties
methods
Classes were introduced in ES6 (ECMAScript 2015).
Basic Syntax
class ClassName {
}
Example:
class Car {
}
This class is just a design.
It doesn't create any object yet.
3.Creating Objects from a Class
To create an object we use the new keyword.
Example:
class Car {}
const car1 = new Car()
const car2 = new Car()
Here:
car1andcar2are objectsBoth are created from the Car class
4.Constructor Method
The constructor is a special method inside a class.
It runs automatically when an object is created.
We use it to initialize properties.
Syntax
constructor(parameters) {
}
Example:
class Car {
constructor(brand, color) {
this.brand = brand
this.color = color
}
}
Explanation:
brand→ input valuecolor→ input valuethis.brand→ property of the object
Creating Objects Using Constructor
const car1 = new Car("Toyota", "Red")
const car2 = new Car("BMW", "Black")
console.log(car1)
console.log(car2)
/*
Car { brand: "Toyota", color: "Red" }
Car { brand: "BMW", color: "Black" }*/
Output conceptually:
Car { brand: "Toyota", color: "Red" }
Car { brand: "BMW", color: "Black" }
Each object stores its own data.
Methods Inside a Class
Classes can also contain methods.
Methods define behavior or actions of objects.
Example:
class Car {
constructor(brand, color) {
this.brand = brand
this.color = color
}
start() {
console.log(this.brand + " car started")
}
stop() {
console.log(this.brand + " car stopped")
}
}
Using Methods
const car1 = new Car("Toyota", "Red")
car1.start()
car1.stop()
/*
Toyota car started
Toyota car stopped*/
Example: Person Class
Let’s create a simple Person class.
class Person {
constructor(name, age) {
this.name = name
this.age = age
}
introduce() {
console.log("My name is " + this.name)
}
}
Create objects:
const p1 = new Person("Rahul", 21)
const p2 = new Person("Amit", 25)
p1.introduce()
p2.introduce()
Encapsulation
Encapsulation means combining data and functions inside a class.
Instead of accessing variables directly, we use methods to interact with them.
Example:
class BankAccount {
constructor(balance) {
this.balance = balance
}
deposit(amount) {
this.balance += amount
}
withdraw(amount) {
this.balance -= amount
}
showBalance() {
console.log("Balance:", this.balance)
}
}
Usage:
const account = new BankAccount(1000)
account.deposit(500)
account.showBalance()
/*
Balance: 1500
*/
Here:
balanceis the datadeposit()andwithdraw()are methods
Both are encapsulated inside the class.
Why OOP is Important
OOP provides many benefits.
1. Code Reusability
You can create multiple objects from one class.
Example:
Student class
↓
student1
student2
student3
2. Better Code Organization
Large applications become easier to manage.
3. Real-World Modeling
We can represent real-world systems easily.
Examples:
User
Product
Order
Student
4. Maintainable Code
When the application grows, OOP helps keep the code structured.
#Assignment
Try implementing this yourself.
Step 1: Create Student Class
class Student {
constructor(name, age) {
this.name = name
this.age = age
}
showDetails() {
console.log("Name:", this.name)
console.log("Age:", this.age)
}
}
Step 2: Create Multiple Objects
const s1 = new Student("Rahul", 21)
const s2 = new Student("Amit", 22)
const s3 = new Student("Neha", 20)
Step 3: Call the Method
s1.showDetails()
s2.showDetails()
s3.showDetails()
#Common Beginner Mistakes
1. Forgetting new
Wrong:
const car = Car()
Correct:
const car = new Car()
2. Forgetting this
Wrong:
brand = brand
Correct:
this.brand = brand
3. Writing functions outside class accidentally
Methods must be inside the class.
#Interview Questions (Very Important)
1. What is Object-Oriented Programming?
OOP is a programming paradigm where code is organized using objects that represent real-world entities.
Objects contain properties and methods.
2. What is a class in JavaScript?
A class is a blueprint used to create objects.
It defines properties and methods.
3. What is an object?
An object is an instance of a class.
Example:
class Car → blueprint
car1 → object
4. What is a constructor?
A constructor is a special method inside a class used to initialize object properties.
It runs automatically when an object is created.
5. What does this keyword mean?
this refers to the current object instance.
Example:
this.name
means the name property of the current object.
6. What is encapsulation?
Encapsulation means combining data and methods inside a single unit (class).
It protects and organizes data.
7. What is the difference between class and object?
| Class | Object |
|---|---|
| Blueprint | Instance |
| Template | Real entity |
| Defines structure | Contains actual values |
8. Why do we use OOP?
We use OOP to:
organize code
reuse code
model real-world entities
improve maintainability
Final Thoughts
Object-Oriented Programming is a fundamental concept in software development.
Understanding OOP helps developers build scalable and maintainable applications.
Key points:
Class → Blueprint
Object → Instance
Constructor → Initializes properties
Methods → Define behavior
Encapsulation → Bundles data and behavior together
Once you understand these basics, you can move to advanced concepts like:
Inheritance
Polymorphism
Abstraction



