DEV Community

Cover image for Understanding Object-Oriented Programming (OOP) in JavaScript
henry messiah tmt
henry messiah tmt

Posted on

Understanding Object-Oriented Programming (OOP) in JavaScript

Introduction

Programming is much like building a city. You don’t throw bricks, cement, and metal randomly together, instead you carefully organize them into structures like houses, schools, and hospitals. In the same way, coding requires structure and organization to make programs easy to build, extend, and maintain.

This is where Object-Oriented Programming (OOP) comes in. OOP is a programming paradigm that allows developers to model real-world entities as objects with properties (data) and behaviors (functions). In JavaScript, OOP is extremely powerful because it makes your code reusable, scalable, and easier to understand.

Understanding OOP in JavaScript is important because it gives you the foundation to work on complex projects, collaborate with teams, and write clean, modular code that mirrors real-life situations.

In this tutorial, we aim to simplify the concept of Object-Oriented Programming (OOP) and make it easy to understand for learners at all levels. By the end of this guide, readers will not only grasp the fundamental principles of OOP but will also be able to confidently apply them in building both simple and complex projects.

What is Object-Oriented Programming (OOP)?

Object-Oriented Programming (OOP) is a way of writing code where you structure it around objects. An object is a collection of properties (attributes) and methods (functions) that describe and control a specific thing.
JavaScript, unlike some traditional OOP languages like Java or C++, is prototype-based, but with the introduction of ES6, we now have the class syntax that makes OOP in JavaScript more familiar and readable.

Core Principles of OOP in JavaScript

There are four main pillars of OOP. Let’s break them down:

  1. Encapsulation: Bundling related data and methods together inside a single object while restricting direct access to certain parts.
  2. Abstraction: Hiding complex details and exposing only the essentials.

  3. Inheritance: Allowing one class (child) to acquire properties and methods of another (parent).

  4. Polymorphism: The ability for the same method or function to behave differently depending on the context.

Real-World Analogy of OOP

Imagine you’re designing a Car System:
Encapsulation: The car has properties like color, brand, and engineType, and methods like startEngine() or applyBrakes(). You don’t directly control how the engine works internally, you just start it.
Abstraction: You turn the steering wheel to change direction but you don’t need to know the complex physics behind it.
Inheritance: A SportsCar can inherit features of a general Car but have additional features like turboBoost().
Polymorphism: The method drive() can mean different things depending on whether it’s a Car, a Bike, or a Truck.
This structure makes the car system easier to understand, expand, and maintain. The same applies to code.

OOP in JavaScript with Examples

  1. Encapsulation (Creating a Class) Encapsulation means bundling data (properties) and methods (functions) into one unit (class).
// Encapsulation example with a Car class
class Car {
  // The constructor is used to initialize object properties
  constructor(brand, color) {
    this.brand = brand; // property to store the car brand
    this.color = color; // property to store the car color
  }

  // Method to start the car engine
  startEngine() {
    // Access the car's brand property
    console.log(`${this.brand} engine started!`);
  }
}

// Create a new object (instance) of the Car class
const myCar = new Car("Toyota", "Red");

// Call the method to start the engine
myCar.startEngine(); // Output: Toyota engine started!
Enter fullscreen mode Exit fullscreen mode

2. Abstraction
Abstraction hides unnecessary details and exposes only the essential methods.

// Abstraction example with a BankAccount class
class BankAccount {
  constructor(owner, balance) {
    this.owner = owner; // account owner's name

    // Private variable to hold the balance
    // It cannot be accessed directly outside the class
    let _balance = balance;

    // Method to check current balance (controlled access)
    this.getBalance = function() {
      return _balance;
    };

    // Method to deposit money (changes balance internally)
    this.deposit = function(amount) {
      _balance += amount;
      console.log(`Deposited ${amount}. New balance: ${_balance}`);
    };
  }
}

// Create a new bank account with an initial balance of 1000
const account = new BankAccount("Peace", 1000);

// Deposit money using the provided method
account.deposit(500); // Output: Deposited 500. New balance: 1500

// Access the balance safely through the method
console.log(account.getBalance()); // Output: 1500

// Direct access like account._balance is not possible (hidden!)
Enter fullscreen mode Exit fullscreen mode

3. Inheritance
Inheritance allows one class to acquire the properties and methods of another.

// Parent class Vehicle
class Vehicle {
  constructor(type) {
    this.type = type; // type of vehicle (e.g., Car, Bike, Truck)
  }

  // Method for movement
  move() {
    console.log(`${this.type} is moving...`);
  }
}

// Child class Bike extends Vehicle
class Bike extends Vehicle {
  constructor(type, brand) {
    // super() calls the parent class constructor
    super(type);
    this.brand = brand; // brand of the bike
  }

  // Override the parent move() method with custom behavior
  move() {
    console.log(`${this.brand} ${this.type} is speeding away!`);
  }
}

// Create an instance of Bike
const myBike = new Bike("Motorbike", "Yamaha");

// Call the overridden method
myBike.move(); // Output: Yamaha Motorbike is speeding away!
Enter fullscreen mode Exit fullscreen mode

4. Polymorphism
Polymorphism allows the same method name to behave differently depending on the object.

// Parent class Animal
class Animal {
  // Default method for speaking
  speak() {
    console.log("This animal makes a sound.");
  }
}

// Child class Dog overrides speak()
class Dog extends Animal {
  speak() {
    console.log("Woof! Woof!");
  }
}

// Child class Cat overrides speak()
class Cat extends Animal {
  speak() {
    console.log("Meow!");
  }
}

// Create an array of different animals
const animals = [new Dog(), new Cat(), new Animal()];

// Each object responds differently to the same method call
animals.forEach(animal => animal.speak());

/* Output:
Woof! Woof!
Meow!
This animal makes a sound.
*/
Enter fullscreen mode Exit fullscreen mode

Importance Of OOP Matters in JavaScript

1. Code Reusability
• With OOP, you can create reusable classes or objects that can be used across different parts of your application.
• Example: A User class with login/logout methods can be reused in multiple projects.
2. Modularity (Better Organization)
• OOP helps break down complex code into smaller, manageable pieces (classes/objects).
• Each object focuses on one specific task, making the codebase easier to navigate and maintain.
3. Encapsulation (Data Hiding & Security)
• OOP allows you to hide the internal implementation of an object and expose only what’s necessary.
• Example: Instead of directly changing a bank account’s balance, you might only allow deposits/withdrawals through specific methods.
4. Inheritance (Avoiding Duplication)
• You can create a parent class with common properties and methods, and let child classes inherit them.
• This reduces duplication of code and enforces consistency.
• Example: A Vehicle class → extended by Car, Bike, Bus.
5. Polymorphism (Flexibility & Extensibility)
• Objects can share the same interface but behave differently based on the context.
• Example: A draw() method might render a circle, square, or triangle differently depending on the object.
6. Improved Maintainability
• Since OOP structures your code in clear, self-contained objects, it becomes easier to fix bugs, add features, or make changes without breaking other parts.

7. Scalability (Good for Large Applications)
• Large projects (like web apps, games, and enterprise software) benefit from OOP because it makes it easier to manage growing codebases.
8. Real-World Modeling
• OOP mirrors real-world entities (users, products, orders, etc.), making the design more intuitive.
• Example: In an e-commerce app, you can have classes for Product, Cart, and Order that closely resemble real-world objects.

Conclusion

Object-Oriented Programming (OOP) in JavaScript is not just a concept — it’s a powerful way to think about code. By understanding encapsulation, abstraction, inheritance, and polymorphism, you can write cleaner, smarter, and more professional programs.
OOP is like building blocks — once you master them, you can create applications as small as a calculator or as big as a social media platform.

Top comments (10)

Collapse
 
adika_david_f4882f8904c91 profile image
ADIKA DAVID

Great write-up, Henry! The real-world analogies (especially the car system example) made the OOP concepts much clearer and relatable. I also like how you broke down encapsulation, abstraction, inheritance, and polymorphism in a beginner-friendly way. This will definitely help a lot of learners approach JavaScript OOP with more confidence. Looking forward to more tutorials from you. Cheers

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thank you very much

Collapse
 
ebiweni_lokoja_f8e97af1f5 profile image
EBIWENI LOKOJA

When it comes to programming nobody does it better.
Thank you for the eye opener.
I'm learning every day

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thanks for the nice comments

Collapse
 
ebiasuodefelix_konyefa_4 profile image
Ebiasuode felix konyefa

Very impressive and carefully written…
👍👍

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thanks

Collapse
 
richard_ben_2ca6ff6b3c8ac profile image
Richard Ben

Very interesting write up, thanks for sharing Henry.

Collapse
 
henry_messiahtmt_099ca84 profile image
henry messiah tmt

Thanks man

Collapse
 
bob_junior_06e527a9bec1ab profile image
Dr Mark

This is the best OOP in JavaScript article I’ve ever seen. This article is both enthralling as well as educative with key concepts well explained in a simplified manner. Thanks for sharing this great piece

Collapse
 
justin_ikakah_4fe4f968621 profile image
Justin Ikakah

Great piece of work