# Understanding Object-Oriented Programming in JavaScript

As programs grow larger, managing code becomes difficult if everything is written in a single place. Developers solve this by organizing code using **Object-Oriented Programming**, usually called **OOP**.

OOP is a way of structuring programs using **objects that represent real-world things**.

Instead of writing random functions and variables everywhere, OOP allows us to group **data and behavior together**.

### Real-World Analogy

A simple way to understand OOP is using the idea of a **blueprint**.

Think about a **car factory**.

Before a car is created, engineers first design a **blueprint** that describes:

*   color
    
*   engine
    
*   speed
    
*   features
    

Using this blueprint, the factory can produce **many cars**.

Each car is an **object**, but all of them follow the same blueprint.

In programming:

*   **Class → Blueprint**
    
*   **Object → Actual product created from the blueprint**
    

`Object diagram`

![](https://cdn.hashnode.com/uploads/covers/69785a22373bd4608f33fced/756bf732-4cc5-4944-9ead-2b8884b61db2.png align="center")

This helps Us understand that **classes define structure and objects are instances of that structure**.

### What is a Class in JavaScript

In JavaScript, a **class** is used to define a blueprint for creating objects.

It describes what properties and behaviors an object will have.

Example:

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

Here we created a class called **Person**. The class describes what information every person object will contain.

### Creating Objects from a Class

Once a class is defined, we can create objects using the **new** keyword.

Example:

```plaintext
let person1 = new Person("Alex", 25);
let person2 = new Person("Emma", 30);

console.log(person1);
console.log(person2);
```

Each object stores its own data but follows the structure defined by the class.

`Instance relationship`

![](https://cdn.hashnode.com/uploads/covers/69785a22373bd4608f33fced/5afc61b5-c3dd-444b-a79f-01f793be50ac.png align="center")

### Constructor Method

Inside a class, we often define a **constructor**.

The constructor runs automatically whenever a new object is created.

It is usually used to initialize values.

Example:

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

Here `this` refers to the object being created.

So when a new object is created, the constructor assigns the values to that object.

### Methods Inside a Class

Classes can also contain **methods**, which are functions that belong to the object.

Example:

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

  showDetails() {
    console.log(this.name + " is " + this.age + " years old");
  }
}
```

Now we can create objects and call the method.

```plaintext
let student1 = new Student("Rahul", 20);
student1.showDetails();
```

Output:

```plaintext
Rahul is 20 years old
```

### Basic Idea of Encapsulation

Encapsulation simply means **keeping related data and behavior together inside a class**.

Instead of having variables and functions scattered across the program, they are grouped inside an object.

This makes code:

*   easier to understand
    
*   easier to maintain
    
*   reusable
    

Encapsulation is one of the core ideas behind Object-Oriented Programming.

### Conclusion

Object-Oriented Programming helps developers organize programs by modeling real-world entities as objects. By using classes as blueprints, we can create multiple objects that share the same structure and behavior. This approach makes programs easier to manage, reuse, and expand as they grow larger.
