# Understanding Object-Oriented Programming in JavaScript

As programs grow bigger and more complex, organizing code becomes very important. Writing everything in one place quickly becomes messy and hard to maintain.

In this particuler blog we discuss about most widely and real world relatable programming paradigm so before moving on topic first understand what is programming paradigm.

## Programming paradigm

Programming paradigm is nothing but way of writing the code, like functional programming paradigm, procedural programming paradigm, object oriented programming paradigm.

Understand with a small example suppose we want to add two number.

```javascript
// procedural programming paradigm
let a = 10;
let b = 30;

let c = a + b;
console.log(c);

// functional programming paradigm
function add(a, b){
 return a + b;
}

let res = add(5, 8);
console.log(res);

// Object Oriented programming paradigm we in below
```

### Real world problem

Imagine you are building a system to manage **students in a college**.

Each student has:

*   name
    
*   age
    
*   course
    
*   roll number
    

If we write code without proper structure, we might end up doing something like this or follow other programming paradigm

```javascript
let student1Name = "Rahul";
let student1Age = 20;
let student1Course = "CSE";

let student2Name = "Vishal";
let student2Age = 21;
let student2Course = "IT";
```

Now imagine managing **100 students** like this.

Problems appear quickly:

*   Too many variables
    
*   Hard to manage data
    
*   Difficult to reuse logic
    
*   Code becomes messy
    

We need a better way to represent **real-world entities** in programming.

## Introducing Object-Oriented Programming (OOP)

Object-Oriented Programming solves this problem by organizing code around **objects**.

Instead of thinking only about functions, we start thinking about **things (objects)** real world entity.

![](https://cdn.hashnode.com/uploads/covers/698b6a86c1c9ef5193601692/5aa4981e-03ea-4504-be8d-2c4af23c8989.png align="center")

For example:

*   Student
    

Each object contains:

*   **Properties** → data
    
*   **Methods** → actions
    

This approach makes programs **more organized and easier to maintain**.

## Understand Class and Object

## What is class?

A **class** is a blueprint or template used to create objects, It defines what properties and methods an object will have.

Class is like user datatype , so creating a class no such memory is allocate.

Example - Student, Car, Person..etc

```javascript
class Student {
// properties & methode
}
```

### What is Object?

An **object** is an instance of a class, It is the actual thing created using the blueprint.

We can create multiple object using same class and when object is create then only memory is allocate and reference is assign to variable.

```javascript
let student1 = new Student();
```

### Constructor in JavaScript

A **constructor** is a special method used to initialize objects when they are created.

It runs automatically when a new object is created or **instantiated.** and constructor is created by using constructor keyword.

```javascript
class Student {
 // construtor creation
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}

// creating object 
let student1 = new Student("Vishal", 21);

console.log(student1.name);
// Output - "Vishal"
```

## Example: Complete OOP Structure

```javascript
class Student {

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

  introduce() {
    console.log(`Hi, my name is ${this.name}`);
  }

}

let student1 = new Student("Vishal", 21, "Computer Science");

student1.introduce();

// Output
"Hi, my name is Vishal"
```

## Pillars of Object-Oriented Programming

There are four principal of object oriented programming called as piller of OOPS.

*   Encapsulation
    
*   Abstraction
    
*   Inheritence
    
*   Polymorphism
    

### Encapsulation

Encapsulation means bundling **data and methods** together inside a class, or its wrap the data member and member function are wrap into single class.

It helps protect data and control how it is accessed.

![](https://cdn.hashnode.com/uploads/covers/698b6a86c1c9ef5193601692/b81974e4-fe55-462c-bed7-f066e1f7640d.png align="center")

### Abstraction

Abstraction means **hiding complex details** and showing only what is necessary.

Basically in OOPs we keep all the data member inside private and all the methode are public so that from outside we can access data via methode not directly.

**Example**

When you drive a car, you only use:

*   steering
    
*   accelerator
    
*   brake
    

You don't need to know **how the engine works internally**.

## Conclusion

Object-Oriented Programming (OOP) helps us organize code by modeling real-world entities using **classes and objects**. Instead of writing scattered variables and functions, we structure programs around **data (properties) and behavior (methods)**. Concepts like **constructors, encapsulation, and abstraction** make code more manageable and easier to scale. By understanding these fundamentals, you build a strong foundation for writing cleaner and more maintainable JavaScript applications.
