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 →
- 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 would you build a simple factory in Python without a long if-elif chain?
Keep a dictionary that maps names to classes and look the class up. Adding a new type is a one-line registration rather than another elif branch.
IntermediateQuick answer1 min read - What is the difference between the Factory Method and Abstract Factory patterns?
Factory Method lets a subclass decide which single product to create. Abstract Factory creates a whole family of related products that are meant to be used together.
IntermediateQuick answer1 min read - Why do APIs use static factory methods like List.of() instead of constructors?
Static factory methods have names, can return cached or existing objects, can return a subtype, and can hide the implementation class. Constructors can do none of these.
AdvancedQuick answer1 min read