Skip to content
Sharp

Object-oriented programming · interview question

How does the use of composition support the open/closed principle in Java?

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

Correct answer

C. By allowing objects to be composed with new behavior without altering existing code.

Why

Composition supports the open/closed principle by enabling the addition of new behavior through composing objects rather than altering existing code. This means systems can be extended with new functionality without modifying existing classes, thereby reducing the risk of introducing bugs.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →