Skip to content
Sharp

Object-oriented programming · interview question

Why should you prefer composition over inheritance when designing classes?

Multiple choiceHardchapter-2-curatedpremiumoopbasic-modeling
Choose one answer
Answer and explanation

Correct answer

A. To allow for more flexible code reuse.

Why

Composition allows for more flexible code reuse by allowing objects to be composed of other objects, rather than extending from a single class. This avoids the rigidity of deep inheritance hierarchies and promotes modular design.

Test yourself on Object-oriented programming

A free test with instant, explained results. No account needed.

Start free test
All Object-oriented programming questions →