Skip to content

A complete guide to Object-Oriented Programming (OOP) concepts in JavaScript with practical examples and interview-ready notes.

Notifications You must be signed in to change notification settings

Kunalsahuji/oops_JS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ OOPs in JavaScript

A beginner-friendly and interview-ready repository to understand Object-Oriented Programming (OOP) concepts in JavaScript with clear definitions, practical examples, and real-world use cases.


🎯 What You'll Learn

  • Core OOP principles in JavaScript
  • ES6 Classes & Objects
  • Encapsulation, Abstraction, Inheritance, Polymorphism
  • Static methods & this keyword
  • Real-world OOP-based examples
  • Clean folder structure for easy learning

πŸ“š Ideal For

  • JavaScript beginners
  • MERN / Full Stack developers
  • Interview preparation
  • Students & freshers
  • Developers revising OOP concepts

JavaScript ECMAScript Language

OOP Encapsulation Abstraction Inheritance Polymorphism



πŸ”₯ OOPs in JavaScript – Complete Notes/pdf with Practical Examples

πŸ“Œ What is OOP?

Object-Oriented Programming (OOP) is a programming paradigm based on objects, which contain:

  • Properties (data)
  • Methods (functions)

Why OOP?

OOP helps in:

  • Code reusability
  • Better structure
  • Easy maintenance
  • Real-world modeling

1️⃣ Object

πŸ“– Definition

An object is a collection of related data and functions stored as key–value pairs.

βœ… Example

const user = {
  name: "Kunal",
  role: "Full Stack Developer",
  greet() {
    console.log(`Hello, I am ${this.name}`);
  }
};

user.greet();

2️⃣ Class

πŸ“– Definition A class is a blueprint for creating objects.

βœ… Example

class User {
  constructor(name, role) {
    this.name = name;
    this.role = role;
  }

  greet() {
    console.log(`Hi, I am ${this.name}`);
  }
}

const user1 = new User("Kunal", "Developer");
user1.greet();

3️⃣ Constructor

πŸ“– Definition A constructor is a special method that runs automatically when an object is created.

βœ… Example

class Car {
  constructor(brand, speed) {
    this.brand = brand;
    this.speed = speed;
  }
}

const car1 = new Car("BMW", 220);

4️⃣ Encapsulation

πŸ“– Definition Encapsulation means binding data and methods together and restricting direct access to data.

βœ… Example

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

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

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

const acc = new BankAccount();
acc.deposit(5000);
console.log(acc.getBalance());

βœ” Data hiding

βœ” Access via methods only


5️⃣ Abstraction

πŸ“– Definition Abstraction means hiding internal implementation details and exposing only necessary functionality.

βœ… Example

class Payment {
  pay() {
    console.log("Processing payment...");
  }
}

class UPI extends Payment {
  pay() {
    console.log("Paid using UPI");
  }
}

const payment = new UPI();
payment.pay();

βœ” User does not care how it works

βœ” User only cares what it does


6️⃣ Inheritance

πŸ“– Definition Inheritance allows one class to reuse properties and methods of another class.

βœ… Example

class Person {
  constructor(name) {
    this.name = name;
  }

  walk() {
    console.log("Walking...");
  }
}

class Developer extends Person {
  code() {
    console.log("Coding...");
  }
}

const dev = new Developer("Kunal");
dev.walk();
dev.code();

7️⃣ Polymorphism

πŸ“– Definition Polymorphism means same method name but different behavior.

βœ… Example

class Shape {
  area() {
    return 0;
  }
}

class Square extends Shape {
  area() {
    return 5 * 5;
  }
}

class Circle extends Shape {
  area() {
    return 3.14 * 5 * 5;
  }
}

const shapes = [new Square(), new Circle()];
shapes.forEach(shape => console.log(shape.area()));

8️⃣ this Keyword

πŸ“– Definition this refers to the current object.

βœ… Example

class User {
  constructor(name) {
    this.name = name;
  }

  show() {
    console.log(this.name);
  }
}

9️⃣ Static Methods

πŸ“– Definition Static methods belong to the class, not to objects created from the class.

βœ… Example

class MathUtils {
  static add(a, b) {
    return a + b;
  }
}
console.log(MathUtils.add(5, 3));

πŸ” Real-World Example (Mini Project) User Management System

class User {
  constructor(username) {
    this.username = username;
  }

  login() {
    console.log(`${this.username} logged in`);
  }
}

class Admin extends User {
  deleteUser(user) {
    console.log(`${user.username} deleted`);
  }
}

const admin = new Admin("Admin1");
const user = new User("Kunal");

admin.login();
admin.deleteUser(user);

πŸ“ Recommended Folder Structure

oops-js/
β”‚
β”œβ”€β”€ 01_object/
β”‚   └── object.js
β”‚
β”œβ”€β”€ 02_class/
β”‚   └── class.js
β”‚
β”œβ”€β”€ 03_constructor/
β”‚   └── constructor.js
β”‚
β”œβ”€β”€ 04_encapsulation/
β”‚   └── encapsulation.js
β”‚
β”œβ”€β”€ 05_abstraction/
β”‚   └── abstraction.js
β”‚
β”œβ”€β”€ 06_inheritance/
β”‚   └── inheritance.js
β”‚
β”œβ”€β”€ 07_polymorphism/
β”‚   └── polymorphism.js
β”‚
β”œβ”€β”€ 08_static/
β”‚   └── static.js
β”‚
β”œβ”€β”€ 09_real_world_examples/
β”‚   └── user-management.js
β”‚
β”œβ”€β”€ NOTES.md
└── README.md

πŸ“Œ Notes for Interview Preparation

  • OOP improves scalability and maintainability

  • JavaScript uses prototype-based inheritance

  • class is syntactic sugar over prototypes

  • Private fields use #

  • Polymorphism is achieved using method overriding


πŸš€ Author

Kunal Sahu Full Stack Developer | MERN | JavaScript


πŸ™Œ Connect with Me


About

A complete guide to Object-Oriented Programming (OOP) concepts in JavaScript with practical examples and interview-ready notes.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published