Interview Q&A / Object-Oriented Programming
Object-Oriented Programming interview questions
The four pillars, interfaces, abstract classes and design basics.
Prefer rapid revision? Practise Object-Oriented Programming short interview answers →
- How do you do dependency injection in JavaScript without any framework?
Pass the dependency as an argument, either to a factory function or a class constructor. A function that receives what it needs is already using dependency injection.
BeginnerQuick answer1 min read - Why use a factory instead of calling new directly?
A factory hides which concrete class is created and how. Callers ask for what they need, and creation rules can change in one place without touching the callers.
BeginnerQuick answer1 min read - How can polymorphism replace a long if-else or switch chain?
Give each case its own class with the same method, then call that method on the object. Adding a new case means adding a class, not editing the old, tested chain.
IntermediateQuick answer1 min read - How do you make a property private in JavaScript classes?
Prefix the field with
IntermediateQuick answer1 min read - Show the Open/Closed Principle in JavaScript with a list of validation rules.
Keep rules as a list of small functions and run them all with one loop. A new rule is added by appending a function, and the validating code is never changed.
IntermediateQuick answer1 min read - Why must you unsubscribe observers, and how do you design for it?
A subject holds strong references to its observers, so a forgotten one stays in memory and keeps receiving events. Return an unsubscribe function from subscribe() and call it when done.
IntermediateQuick answer1 min read