Skip to content
Sharp

Object-oriented programming · interview question

In designing a payment processing module, you need to handle credit card and PayPal transactions. Why might you choose composition over inheritance to model this?

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

Correct answer

C. Composition allows flexibility to add new payment methods without altering existing code.

Why

Composition allows each payment method to be a separate component that can be easily replaced or extended without modifying the existing hierarchy. This leads to more maintainable and flexible code, adhering to the open/closed principle.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →