Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 1 of 10 · Object-Oriented JavaScript
Interview question

What are the four main principles of OOP and how does JavaScript support them? OOP के चार मुख्य principles क्या हैं और JavaScript उन्हें कैसे support करता है?

Answer
PrincipleMeaningHow JavaScript supports it
EncapsulationBundling data and methods, hiding internal detailsPrivate fields (#field), closures
AbstractionExposing only relevant details, hiding complexityPublic methods hiding private implementation
InheritanceReusing behavior from a parent classextends, prototype chain
PolymorphismDifferent classes respond differently to the same method callMethod 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 करता है
EncapsulationData-methods bundle करना, internal details छुपानाPrivate fields (#field), closures
Abstractionसिर्फ relevant details expose करनाPublic methods से private implementation छुपाना
InheritanceParent class से behavior reuse करनाextends, prototype chain
Polymorphismअलग classes का same method call पर अलग responseMethod 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?