Skip to content
Sharp

Object-oriented programming · interview question

In an e-commerce application, why might you prefer to use composition over inheritance for implementing a 'DiscountStrategy'?

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

Correct answer

C. To allow dynamic selection of strategies at runtime.

Why

Composition allows you to implement a 'DiscountStrategy' as an interface that can be implemented by various classes. This flexibility enables the dynamic selection and combination of different strategies at runtime, adhering to the strategy pattern.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →