Skip to main content

Command Palette

Search for a command to run...

What Object-Oriented Programming (OOP) Means

Updated
5 min readView as Markdown
What Object-Oriented Programming (OOP) Means
J
Aspiring software developer documenting my journey in building, learning, and understanding technology. Computer Science student at NSUT Delhi.

Object Oriented Programming, heard of this word but never gets what exactly is this? No worries let's break it down today So you know what is programming, you know the meaning of orientation? Let's see quickly look over the First word that creating problem that is

OBJECT:

let objectExample = {
    dataType : "object",
    language : "JavaScript",
    year : 2026,
}

Object is a data type in js, that stores data as key-value pairs.
In the above example,
year is Key and 2026 is value.
Objects can also have a function see the syntax below

let object = {

     // key and value pair
        
     functionName(){
        // What to Do ?
    }
}

See the example below.

let objectExample = {
     dataType : "object",
     language : "JavaScript",
     year : 2026,
    
     greet(){    
        console.log(`Welcome in ${objectExample.year}`)
    }
}


objectExample.greet()  // Welcome in 2026

That is the basic about object for more you can refer to this Blog, however the things we will need to understand oops, will see on the go.

Now let's understand the Object-Oriented Programming In normal programming we often create variables and function etc but in OOPs, we bind different properties, variables, functions together calling them a class

class TataCar {
    showCompany() {
         console.log("This is Tata company's car");
     }
}

    

Yes creating class is like creating objects, but the difference between object and class is that class acts as a blueprint and we can have different instance for one class with different values. See like for a particular model of car there is a blueprint

Now using this blueprint only multiple cars are designed, that designed cars are called instances of the blueprint, those instances may have different color or features but ultimately the working will be same as of the blueprint.

Similarly we design a class, and using this class multiple instances can be created

class TataCar {
    showCompany() { 
        console.log("This is a car from Tata Motors");
     }

    startEngine() {
        console.log("Engine started");
     }

}
const car1 = new TataCar();
const car2 = new TataCar();

car1.showCompany();     //  This is a car from Tata Motors
car1.startEngine();    //   Engine started

car2.showCompany();    //   This is a car from Tata Motors

When creating the instances of a class we use the new keyword.

Remember above we discussed that the cars made from blueprint may have different colors or some properties how is it done?
Constructor method is what used for this.

Constructor :

It is a special type of function that used to create and initialize an object instance of the class with the given value.

class TataCar { 
       
      constructor(color, model) {
        this.color = color;
        this.model = model;
      }       

 
      showCompany() { 
        console.log("This is a car from Tata Motors");
      }
        
      startEngine() {
        console.log("Engine started");
      }

      showDetails(){ 
        console.log("Model:", this.model) 
        console.log("Color:", this.color) 
      }
}

const car1 = new TataCar("Red", "Nexon");
const car2 = new TataCar("Blue", "Harrier");

car1.showDetails();
//    Model: Nexon
//    Color: Red



car2.showDetails();
//    Model: Harrier
//    Color: Blue

this is a topic that deserves separate discussion, but for now just remember that 'this' points to the current instance,
like inside car 1 this.color = color will be interpreted by JS as car1.color = color .
while for 2 this will be point to car2.

Just like a constructor, we may also have other methods (functions) that serves different purposes as demand. Used to perform different operation on classes.
In above examples,
- showCompany() ,
- startEngine(),
- showDetails(),

fall into this category.


Lets take a complete workflow to understand practical use of OOPs.

class Student {
    constructor(name, standard) {
        this.name = name;
        this.standard = standard;
    } 
    
    showInfo(){
        console.log(`My name is \({this.name}, I read in \){this.standard}`)
    }
}

Now we can reuse that code as much as we want, the real power of OOPs

let stud1 = new Student("Ram", 10)
let stud2 = new Student("Krishna", 12)
let stud3 = new Student("Hari", 11)

stud1.showInfo()
stud2.showInfo()
stud3.showInfo()

Encapsulation

When you got a car, do you get a complete blueprint? Usually No.

Here in classes we also hide some of our insiders from users, process is known as encapsulation, means encapsulate(hide) internals variables and functions to prevent their modifications accidentally and can be accesses only by some controlled methods.

Syntax: We use #before name to declare private variables or functions

class BankAccount {
  #balance = 0;   // private data

  deposit(amount) {
    this.#balance += amount;
  }

  getBalance() {
    return this.#balance;
  }
}

const acc = new BankAccount();
acc.deposit(1000);

console.log(acc.getBalance()); // 1000
console.log(acc.#balance); // Will give error 

Note private variables and functions can be accessed by another method of the class but can't be accesses directly outside the class.


Felt amazing, same was my response. Using OOPs greatly increases code reusability
So this is all about the basics of OOPs in JavaScript. Hope you liked it and got very much to learn.