Episode 7 — DSA with JavaScript / 7.4 — Object Oriented Programming
7.4 — Interview Questions: OOP in JavaScript
Beginner
Q1. What is a class in JavaScript?
Answer: A class is a blueprint for creating objects with shared structure and behavior. Introduced in ES6, it's syntactic sugar over JavaScript's prototype-based inheritance.
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return `Hi, I'm ${this.name}`;
}
}
class Person {
public:
string name;
int age;
Person(const string& n, int a) : name(n), age(a) {}
string greet() const { return "Hi, I'm " + name; }
};
Q2. What does the this keyword refer to?
Answer:
this refers to the current object instance. In a method, it points to the
object the method was called on.
class Counter {
constructor() { this.count = 0; }
increment() { this.count++; return this; }
}
const c = new Counter();
c.increment().increment(); // method chaining with this
console.log(c.count); // 2
Pitfall: Arrow functions inherit this from enclosing scope; regular
functions get their own this.
Q3. What is inheritance in OOP?
Answer: Inheritance lets a child class reuse and extend a parent class's properties and methods.
class Animal {
constructor(name) { this.name = name; }
speak() { return `${this.name} makes a sound`; }
}
class Dog extends Animal {
speak() { return `${this.name} barks!`; }
}
Q4. What is the super keyword?
Answer:
super calls the parent class's constructor or methods.
class Cat extends Animal {
constructor(name, color) {
super(name); // must call before using this
this.color = color;
}
speak() {
return super.speak() + " (meow)";
}
}
Q5. How do you make a property private in JavaScript?
Answer:
Use the # prefix (ES2022):
class Secret {
#value;
constructor(v) { this.#value = v; }
getValue() { return this.#value; }
}
const s = new Secret(42);
s.getValue(); // 42
// s.#value; // SyntaxError!
In C++: use the private: access modifier.
Intermediate
Q6. Explain polymorphism with an example.
Answer: Polymorphism means objects of different classes respond to the same method call differently.
class Shape {
area() { throw new Error("Not implemented"); }
}
class Circle extends Shape {
constructor(r) { super(); this.r = r; }
area() { return Math.PI * this.r ** 2; }
}
class Rect extends Shape {
constructor(w, h) { super(); this.w = w; this.h = h; }
area() { return this.w * this.h; }
}
// Polymorphic behavior
[new Circle(3), new Rect(4, 5)].forEach(s => console.log(s.area()));
class Shape {
public:
virtual double area() const = 0; // pure virtual
virtual ~Shape() = default;
};
class Circle : public Shape {
double r;
public:
Circle(double r) : r(r) {}
double area() const override { return 3.14159 * r * r; }
};
Q7. What is encapsulation and why does it matter?
Answer: Encapsulation bundles data and methods together, hiding internal state to prevent external code from putting the object in an invalid state.
Benefits:
- Data integrity (validation in setters)
- Loose coupling (can change internals without breaking users)
- Security (sensitive data not directly accessible)
Q8. Explain static methods. When should you use them?
Answer: Static methods belong to the class, not instances. Use them for utility functions that don't need instance state.
class ArrayUtils {
static sum(arr) { return arr.reduce((a, b) => a + b, 0); }
static avg(arr) { return ArrayUtils.sum(arr) / arr.length; }
}
ArrayUtils.sum([1, 2, 3]); // 6
Q9. What is the difference between composition and inheritance?
Answer:
- Inheritance: "is-a" relationship (Dog is-a Animal)
- Composition: "has-a" relationship (Car has-a Engine)
// Composition (preferred for flexibility)
class Engine { start() { return "vroom"; } }
class Car {
constructor() { this.engine = new Engine(); }
start() { return this.engine.start(); }
}
Rule of thumb: Favor composition over inheritance for flexibility.
Q10. How does JS class inheritance differ from C++?
Answer:
| Feature | JavaScript | C++ |
|---|---|---|
| Single/Multiple | Single only | Multiple supported |
| Virtual dispatch | Always (prototype chain) | Only with virtual keyword |
| Abstract classes | No built-in (throw in base) | Pure virtual = 0 |
| Access modifiers | # for private | public/private/protected |
| Memory | Garbage collected | Manual/RAII/smart pointers |
Advanced
Q11. Implement the Singleton pattern in both JS and C++.
Answer:
class Logger {
static #instance = null;
#logs = [];
static getInstance() {
if (!Logger.#instance) Logger.#instance = new Logger();
return Logger.#instance;
}
log(msg) { this.#logs.push({ time: Date.now(), msg }); }
getLogs() { return [...this.#logs]; }
}
class Logger {
static Logger* instance;
vector<string> logs;
Logger() {}
public:
static Logger& getInstance() {
static Logger inst;
return inst;
}
void log(const string& msg) { logs.push_back(msg); }
};
Q12. What is the prototype chain in JavaScript?
Answer:
Every JS object has an internal [[Prototype]] link. When a property is not
found on the object, JS looks up the chain.
myDog → Dog.prototype → Animal.prototype → Object.prototype → null
const dog = new Dog("Rex");
dog.speak(); // found on Dog.prototype
dog.toString(); // found on Object.prototype
dog.foo; // not found → undefined
Q13. Implement the Observer pattern.
Answer:
class EventEmitter {
#listeners = {};
on(event, fn) {
(this.#listeners[event] ??= []).push(fn);
}
emit(event, ...args) {
for (const fn of this.#listeners[event] ?? []) {
fn(...args);
}
}
off(event, fn) {
this.#listeners[event] = (this.#listeners[event] ?? []).filter(f => f !== fn);
}
}
const emitter = new EventEmitter();
emitter.on("data", (d) => console.log("Got:", d));
emitter.emit("data", 42); // "Got: 42"
Q14. How do you implement abstract classes in JavaScript?
Answer:
JavaScript has no abstract keyword. Throw an error in the base class:
class Shape {
constructor() {
if (new.target === Shape) throw new Error("Cannot instantiate abstract class");
}
area() { throw new Error("area() must be implemented"); }
}
class Circle extends Shape {
constructor(r) { super(); this.r = r; }
area() { return Math.PI * this.r ** 2; }
}
// new Shape(); // Error!
new Circle(5).area(); // 78.53...
Quick-fire table
| # | Question | Answer |
|---|---|---|
| 1 | Class vs object? | Class is blueprint, object is instance |
| 2 | this in arrow fn? | Inherited from enclosing scope |
| 3 | super() required when? | In child constructor before using this |
| 4 | Private in JS? | #field prefix |
| 5 | Static method accessed how? | ClassName.method() |
| 6 | Polymorphism? | Same method, different behavior per type |
| 7 | virtual in C++? | Enables runtime polymorphism |
| 8 | Composition vs inheritance? | has-a vs is-a; prefer composition |
| 9 | Prototype chain? | Object → Proto → Proto → ... → null |
| 10 | Singleton? | One instance, global access point |
Rapid self-check cards
SC-001
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-002
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-003
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-004
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-005
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-006
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-007
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-008
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-009
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-010
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-011
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-012
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-013
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-014
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-015
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-016
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-017
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-018
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-019
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-020
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-021
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-022
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-023
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-024
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-025
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-026
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-027
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-028
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-029
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-030
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-031
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-032
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-033
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-034
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-035
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-036
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-037
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-038
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-039
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-040
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-041
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-042
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-043
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-044
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-045
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-046
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-047
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-048
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-049
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-050
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-051
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-052
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-053
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-054
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-055
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-056
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-057
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-058
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-059
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-060
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-061
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-062
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-063
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-064
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-065
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-066
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-067
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-068
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-069
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-070
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-071
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-072
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-073
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-074
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-075
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-076
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-077
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-078
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-079
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-080
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-081
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-082
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-083
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-084
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-085
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-086
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-087
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-088
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-089
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-090
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-091
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-092
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-093
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-094
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-095
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-096
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-097
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-098
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-099
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-100
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-101
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-102
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-103
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-104
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-105
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-106
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-107
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-108
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-109
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-110
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-111
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-112
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-113
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-114
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-115
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-116
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-117
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-118
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-119
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-120
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-121
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-122
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-123
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-124
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-125
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-126
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-127
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-128
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-129
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-130
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-131
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-132
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-133
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-134
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-135
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-136
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-137
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-138
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-139
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-140
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-141
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-142
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-143
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-144
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-145
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-146
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-147
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-148
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-149
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-150
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-151
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-152
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-153
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-154
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-155
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-156
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-157
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-158
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-159
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-160
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-161
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-162
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-163
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-164
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-165
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-166
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-167
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-168
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-169
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-170
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-171
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-172
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-173
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-174
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-175
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-176
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-177
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-178
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-179
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-180
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-181
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-182
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-183
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-184
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-185
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-186
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-187
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-188
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-189
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-190
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls
SC-191
- Q: What is a class?
- A: A blueprint for creating objects with shared properties and methods
SC-192
- Q: What does
thisrefer to? - A: The current object instance the method is called on
SC-193
- Q: What is inheritance?
- A: Child class inherits properties/methods from parent class
SC-194
- Q: What is
super? - A: Calls the parent class constructor or methods
SC-195
- Q: How to make a field private in JS?
- A: Prefix with # (e.g., #balance)
SC-196
- Q: What is polymorphism?
- A: Same interface, different behavior depending on the type
SC-197
- Q: What is encapsulation?
- A: Bundling data+methods and hiding internals
SC-198
- Q: What is a static method?
- A: Belongs to the class, not instances
SC-199
- Q: Composition vs inheritance?
- A: has-a vs is-a; composition is more flexible
SC-200
- Q: What is method chaining?
- A: Return
thisfrom methods to chain calls