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 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