# Object Oriented Programming in JavaScript (Simple Explanation)

When learning programming, at some point you hear about **OOP** or Object-Oriented Programming. It sounds complicated at first, but the idea is actually pretty simple.

* * *

### What Object-Oriented Programming means

**Object-Oriented Programming (OOP)** is a way of writing code where we organize things into **objects**.

Instead of writing scattered functions and variables, we group related data and behavior together.

This makes code easier to reuse and manage, especially in bigger projects.

* * *

### Real-world analogy (blueprint → objects)

Think of a **car blueprint**.

The blueprint defines:

*   color
    
*   engine
    
*   wheels
    
*   behavior like driving or braking
    

But the blueprint itself is not a car.

Using that blueprint, a factory can create **many cars**.

In programming:

*   **Blueprint → Class**
    
*   **Car → Object**
    

* * *

### What is a class in JavaScript

A **class** is basically a blueprint used to create objects.

Example:

```plaintext
class Person {
}
```

Right now this class doesn’t do much, but it defines the structure for objects we create later.

* * *

### Creating objects using classes

Once a class exists, we can create objects from it.

Example:

```plaintext
class Person {}

const person1 = new Person();
const person2 = new Person();
```

Both `person1` and `person2` are objects created from the same class.

* * *

### Constructor method

The **constructor** runs automatically when a new object is created.

It is usually used to assign values.

Example:

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

Now when we create an object, we can pass data to it.

* * *

### Methods inside a class

Classes can also contain **methods** (functions that belong to the object).

Example:

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

  introduce() {
    console.log("Hi, my name is " + this.name)
  }
}
```

Now each object can use this method.

* * *

### Basic idea of encapsulation

Encapsulation simply means **keeping related data and behavior together**.

For example:

*   `name` and `age` belong to a person
    
*   `introduce()` method also belongs to that person
    

So everything related to a person stays inside the same class.

* * *

### Small assignment example

Let’s create a **Student class**.

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

  printDetails() {
    console.log(this.name + " is " + this.age + " years old")
  }
}

const student1 = new Student("Rahul", 20)
const student2 = new Student("Aisha", 22)

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

Here we created multiple student objects using the same class.

* * *

### Final thought

OOP helps structure code in a clean way. Instead of repeating logic again and again, we create **blueprints (classes)** and generate objects from them.

This makes programs easier to scale and maintain.
