Skip to content
Sharp

Object-oriented programming · interview question

In an e-commerce application, why might you choose composition over inheritance for a 'ShoppingCart' class?

Multiple choiceHardchapter-5-curatedpremiumoopcomposition-vs-inheritance
Choose one answer
Answer and explanation

Correct answer

A. To manage different types of products more flexibly.

Why

Using composition allows 'ShoppingCart' to manage different types of products or services more flexibly, without being restricted by the fixed structure of an inheritance hierarchy.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →