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.
- Core OOP principles in JavaScript
- ES6 Classes & Objects
- Encapsulation, Abstraction, Inheritance, Polymorphism
- Static methods &
thiskeyword - Real-world OOP-based examples
- Clean folder structure for easy learning
- JavaScript beginners
- MERN / Full Stack developers
- Interview preparation
- Students & freshers
- Developers revising OOP concepts
Object-Oriented Programming (OOP) is a programming paradigm based on objects, which contain:
- Properties (data)
- Methods (functions)
OOP helps in:
- Code reusability
- Better structure
- Easy maintenance
- Real-world modeling
An object is a collection of related data and functions stored as keyβvalue pairs.
const user = {
name: "Kunal",
role: "Full Stack Developer",
greet() {
console.log(`Hello, I am ${this.name}`);
}
};
user.greet();π 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();π 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);π 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
π 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
π 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();π 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()));π Definition this refers to the current object.
β Example
class User {
constructor(name) {
this.name = name;
}
show() {
console.log(this.name);
}
}π 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);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-
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
Kunal Sahu Full Stack Developer | MERN | JavaScript
- π LinkedIn
- πΌ GitHub
- π Portfolio
- π§ ksahu0103@gmail.com