# Understanding Object-Oriented Programming in JavaScript

Imagine a car company designing a new car model.

Before building thousands of cars, engineers first create a blueprint. That blueprint describes what every car should have — things like wheels, engine, color, and seats.

Once the blueprint is ready, the factory can produce many cars using that same design.

Programming works in a very similar way.

Instead of repeatedly writing the same structure for similar objects, developers create a blueprint in code. This blueprint is called a class, and the things created from it are called objects.

This concept is part of a programming approach known as Object-Oriented Programming, often shortened to OOP.

## What Object-Oriented Programming (OOP) Means

Object-Oriented Programming is a style of writing code where we organize programs using objects.

An object represents something from the real world and usually contains:

*   Properties --> information about the object
    
*   Methods --> actions the object can perform
    

For example, think about a student.

A student object might contain properties like:

```javascript
name
age
course
```

And it might have actions like:

```javascript
printDetails()
```

Using OOP helps developers:

*   organize code more clearly
    
*   reuse the same structure multiple times
    
*   make programs easier to maintain and expand
    

## Real-World Analogy: Blueprint --> Objects

Let's revisit the car blueprint example.

A blueprint describes how a car should look and behave.

Using that blueprint, a factory can create many cars:

```javascript
Blueprint --> Car 1
Blueprint --> Car 2
Blueprint --> Car 3
```

In programming:

```javascript
Class --> Blueprint
Object --> Actual item created from the blueprint
```

So a **class defines the structure**, and **objects are the real instances created from it**.

## What Is a Class in JavaScript?

In JavaScript, a class is used to define the structure of objects.

It describes what properties and methods objects created from it should have.

Example:

```javascript
class Person {

}
```

This class doesn't do anything yet, but it serves as a template for creating person objects.

## Creating Objects Using Classes

Once a class exists, we can create objects using the `new` keyword.

Example:

```javascript
let person1 = new Person();
```

Here:

```javascript
Person --> class
person1 --> object
```

We can create multiple objects from the same class:

```javascript
let person2 = new Person();
let person3 = new Person();
```

This demonstrates one of the main benefits of OOP: **code reusability**.

## The Constructor Method

When creating objects, we usually want to give them initial values.

That’s where the constructor method comes in.

The constructor runs automatically whenever a new object is created.

Example:

```javascript
class Person {

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

}
```

Explanation:

*   `this.name` refers to the object's name property
    
*   `this.age` refers to the object's age property
    

Now we can create objects with data:

```javascript
let person1 = new Person("Alice", 25);
let person2 = new Person("David", 30);
```

Each object now contains its own values.

## Methods Inside a Class

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

Methods allow objects to perform actions.

Example:

```javascript
class Person {

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

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

}
```

Now we can create an object and call the method:

```javascript
let person1 = new Person("Alice", 25);

person1.greet();
```

Output:

```plaintext
Hello, my name is Alice
```

Methods help objects perform behaviors related to their data.

## Basic Idea of Encapsulation

Encapsulation is a concept where data and related functions are kept together inside a class.

For example, a Student class might contain:

*   student name
    
*   student age
    
*   a method that prints student details
    

Instead of managing these pieces separately in different parts of the code, everything stays inside the class structure.

This approach makes programs:

*   easier to understand
    
*   easier to maintain
    
*   easier to reuse
