Skip to content
Sharp

Object-oriented programming · interview question

When is it more appropriate to use composition over inheritance in Java?

Multiple choiceMediumchapter-5-curatedpremiumoopinheritance-basics
Choose one answer
Answer and explanation

Correct answer

B. When you need to share code among unrelated classes.

Why

Composition is more appropriate when you need to share code among unrelated classes, as it allows for greater flexibility and avoids the issues of forced inheritance and inappropriate is-a relationships.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →