Interview question
What are the four main principles of OOP and how does JavaScript support them? OOP के चार मुख्य principles क्या हैं और JavaScript उन्हें कैसे support करता है?
Answer
| Principle | Meaning | How JavaScript supports it |
|---|---|---|
| Encapsulation | Bundling data and methods, hiding internal details | Private fields (#field), closures |
| Abstraction | Exposing only relevant details, hiding complexity | Public methods hiding private implementation |
| Inheritance | Reusing behavior from a parent class | extends, prototype chain |
| Polymorphism | Different classes respond differently to the same method call | Method overriding |
// ENCAPSULATION - private fields hide internal state
class BankAccount {
#balance; // truly private, accessible only inside this class
constructor(initialBalance) {
this.#balance = initialBalance;
}
deposit(amount) {
this.#balance += amount;
}
getBalance() {
return this.#balance; // controlled access
}
}
const acc = new BankAccount(100);
// console.log(acc.#balance); // SyntaxError - truly inaccessible from outside
console.log(acc.getBalance()); // 100
// INHERITANCE
class Shape {
constructor(name) { this.name = name; }
area() { return 0; } // base implementation
}
// POLYMORPHISM - each subclass overrides area() differently
class Circle extends Shape {
constructor(radius) { super('Circle'); this.radius = radius; }
area() { return Math.PI * this.radius ** 2; }
}
class Square extends Shape {
constructor(side) { super('Square'); this.side = side; }
area() { return this.side ** 2; }
}
const shapes = [new Circle(5), new Square(4)];
shapes.forEach(shape => {
console.log(`${shape.name} area: ${shape.area()}`); // same method call, different behavior
});
// ABSTRACTION - complex logic hidden behind a simple public method
class PaymentProcessor {
processPayment(amount) {
this.#validateAmount(amount);
this.#chargeCard(amount);
this.#sendReceipt();
}
#validateAmount(amount) { /* internal detail */ }
#chargeCard(amount) { /* internal detail */ }
#sendReceipt() { /* internal detail */ }
}| Principle | अर्थ | JavaScript कैसे support करता है |
|---|---|---|
| Encapsulation | Data-methods bundle करना, internal details छुपाना | Private fields (#field), closures |
| Abstraction | सिर्फ relevant details expose करना | Public methods से private implementation छुपाना |
| Inheritance | Parent class से behavior reuse करना | extends, prototype chain |
| Polymorphism | अलग classes का same method call पर अलग response | Method overriding |
// ENCAPSULATION - private fields
class BankAccount {
#balance;
constructor(initialBalance) {
this.#balance = initialBalance;
}
deposit(amount) {
this.#balance += amount;
}
getBalance() {
return this.#balance;
}
}
const acc = new BankAccount(100);
console.log(acc.getBalance()); // 100
// INHERITANCE
class Shape {
constructor(name) { this.name = name; }
area() { return 0; }
}
// POLYMORPHISM
class Circle extends Shape {
constructor(radius) { super('Circle'); this.radius = radius; }
area() { return Math.PI * this.radius ** 2; }
}
class Square extends Shape {
constructor(side) { super('Square'); this.side = side; }
area() { return this.side ** 2; }
}
const shapes = [new Circle(5), new Square(4)];
shapes.forEach(shape => {
console.log(`${shape.name} area: ${shape.area()}`);
});Was this answer clear?