Composition vs inheritance
OOP ยท 8 interview questions
Inheritance and composition both reuse code. Inheritance does it by making one class a kind of another; composition does it by holding an instance and delegating to it.
The standard advice is to favour composition, and the reason is coupling. A subclass depends on its parent's implementation and inherits the whole surface whether it wants it or not. A composed object depends only on the interface it calls, and the relationship can change at run time.
Inheritance still earns its place when the relationship really is substitutable and the parent is designed to be extended. The question to ask is whether callers of the parent would be surprised by the child.
Composition vs inheritance interview questions
- Why is composition usually preferred over inheritance?
- It couples classes through an interface rather than an implementation, avoids inheriting unwanted behaviour, allows the relationship to change at run time, and sidesteps deep hierarchies that are hard to reason about.
- What is delegation?
- Holding another object and forwarding calls to it, optionally adding behaviour. It's the mechanism composition uses to reuse code without inheriting.
- When is inheritance genuinely the right choice?
- When the subtype is truly substitutable for the parent, the parent was designed for extension with a documented contract, and you want polymorphic dispatch over a family of types.
- Why is Stack extending Vector a design mistake?
- A stack promises access only at the top, but inheriting Vector exposes insert-at-index and remove-at-index, so callers can violate the invariant the class exists to enforce. Composition โ a stack holding a list โ would have kept the contract.
- Why they ask: A real example from the Java standard library, which makes the point better than a toy one.
- What can composition do at run time that inheritance cannot?
- Change collaborators. A composed object's dependency can be swapped after construction โ switching a compression strategy while running. Inheritance fixes the relationship at compile time.
- What is a mixin?
- A class or trait providing behaviour intended to be mixed into others rather than instantiated alone. It's a middle ground: reuse without a full is-a claim. Python uses multiple inheritance for it; other languages use traits or default interface methods.
- Why are deep inheritance hierarchies a warning sign?
- Understanding one class means reading every ancestor, behaviour arrives from several levels up, and a change near the root can affect everything below it. Depth beyond two or three levels usually indicates composition was the better tool.
- What does composition cost you?
- Boilerplate. Delegating a wide interface means writing forwarding methods that inheritance would have given for free, which is why some languages provide explicit delegation support.
You'll forget most of this by next week
That's not a discipline problem, it's how memory works. In the app these come back on an expanding schedule โ right before you'd lose them.
Start free for 7 days