# 🏗️ Understanding Object-Oriented Programming (OOP) in JavaScript

When projects grow bigger, writing everything as separate variables and functions becomes messy.

That’s where **Object-Oriented Programming (OOP)** helps.

OOP is a programming style where we organize code using **objects and classes**.

* * *

# 📌 What is Object-Oriented Programming (OOP)?

Object-Oriented Programming is a way of writing code using:

*   Classes (blueprints)
    
*   Objects (real instances)
    
*   Properties (data)
    
*   Methods (functions inside objects)
    

It helps in:

*   Organizing code
    
*   Reusing code
    
*   Making programs scalable
    

* * *

# 🚗 Real-World Analogy: Blueprint → Objects

Think about a **car factory**.

A company creates a **blueprint** for a car.

From that blueprint, they manufacture:

*   Car 1
    
*   Car 2
    
*   Car 3
    

Each car has:

*   Brand
    
*   Color
    
*   Speed
    

The blueprint is like a **class**.  
Each manufactured car is an **object**.

* * *

# 📊 Blueprint → Object Diagram

```plaintext
        Class (Blueprint)
              ↓
        ----------------
        |   Car Class  |
        ----------------
           ↓       ↓       ↓
        Car1    Car2    Car3
       (Object) (Object) (Object)
```

One blueprint → Multiple objects.

* * *

# 🏛️ What is a Class in JavaScript?

A **class** is a template used to create objects.

In JavaScript, we use the `class` keyword.

* * *

## 🔹 Example: Creating a Class

```plaintext
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  greet() {
    console.log("Hello, my name is " + this.name);
  }
}
```

Let’s break it down.

* * *

# 🧱 Constructor Method

The `constructor` is a special method that runs automatically when we create a new object.

It is used to initialize properties.

```plaintext
constructor(name, age) {
  this.name = name;
  this.age = age;
}
```

`this` refers to the current object.

* * *

# 🧍 Creating Objects from a Class

Now we create objects using the `new` keyword.

```plaintext
let person1 = new Person("Ilyas", 21);
let person2 = new Person("Rahul", 22);
```

Now we have two different objects created from the same class.

* * *

# 🔧 Calling Methods

```plaintext
person1.greet();
person2.greet();
```

Output:

```plaintext
Hello, my name is Ilyas
Hello, my name is Rahul
```

Each object has its own data but shares the same structure.

* * *

# 📊 Class → Instance Relationship

```plaintext
Class: Person
   ↓
person1 → { name: "Ilyas", age: 21 }
person2 → { name: "Rahul", age: 22 }
```

Class defines structure.  
  
Objects store actual data.

* * *

# 🧠 Methods Inside a Class

Methods are functions written inside a class.

Example:

```plaintext
class Car {
  constructor(brand, color) {
    this.brand = brand;
    this.color = color;
  }

  drive() {
    console.log(this.brand + " is driving.");
  }
}
```

Here:

*   `brand` and `color` → properties
    
*   `drive()` → method
    

* * *

# 🔒 Basic Idea of Encapsulation

Encapsulation means:

👉 Keeping data and related methods together inside a class.

Instead of writing:

```plaintext
let name = "Ilyas";
let age = 21;

function greet() {
  console.log("Hello " + name);
}
```

We organize it like this:

```plaintext
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  greet() {
    console.log("Hello " + this.name);
  }
}
```

Everything related to a person stays inside the `Person` class.

That’s cleaner and more organized.

* * *

# 🎯 Why OOP is Powerful

*   Code becomes reusable
    
*   Easy to manage large projects
    
*   Clear structure
    
*   Real-world modeling becomes easier
    

Instead of rewriting logic, we reuse the class.

* * *

# 🎯 Mini Assignment

### 1️⃣ Create a Student class

```plaintext
class Student {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  printDetails() {
    console.log("Name: " + this.name + ", Age: " + this.age);
  }
}
```

* * *

### 2️⃣ Create multiple student objects

```plaintext
let student1 = new Student("Aman", 20);
let student2 = new Student("Priya", 21);

student1.printDetails();
student2.printDetails();
```

You just created reusable student objects.

* * *

# 💡 Final Thoughts

Object-Oriented Programming helps you:

*   Structure code properly
    
*   Avoid repetition
    
*   Build scalable applications
    
*   Model real-world systems
    

Almost all modern frameworks (React, Angular, Node.js apps) rely heavily on OOP concepts.

Master this, and your JavaScript level jumps significantly.
