Explain the difference between inheritance and composition and when to prefer each.

Prepare for the Object-Oriented Programming Test with flashcards and multiple choice questions. Each question comes with hints and explanations to enhance understanding. Master OOP concepts and succeed in your exam!

Multiple Choice

Explain the difference between inheritance and composition and when to prefer each.

Explanation:
Two ways objects relate in OOP are is-a versus has-a. Inheritance creates an is-a relationship: a subclass inherits the interface and behavior of a base class, so you can treat instances of the subclass as instances of the base type and rely on shared implementation. Composition creates a has-a relationship: an object is built by including other objects and delegating work to them, rather than by inheriting from a superclass. This builds flexible, modular behavior, because you can swap components, replace implementations, and change how things are assembled without altering the external interface of the containing object. You should prefer composition when you want flexibility and loose coupling. It lets you assemble and reconfigure behavior from interchangeable parts, making it easier to adapt to changing requirements and to test components in isolation. Use inheritance when there’s a clear, stable hierarchy and you want to share a common interface and implementation across many related types, enabling polymorphism. For example, a Car has an Engine, so it’s composed of components; a Dog is an Animal, so it inherits from Animal to reuse and specialize its behavior. Remember, composition isn’t only about code reuse, and inheritance and composition aren’t the same—each serves different relationship models and design goals.

Two ways objects relate in OOP are is-a versus has-a. Inheritance creates an is-a relationship: a subclass inherits the interface and behavior of a base class, so you can treat instances of the subclass as instances of the base type and rely on shared implementation.

Composition creates a has-a relationship: an object is built by including other objects and delegating work to them, rather than by inheriting from a superclass. This builds flexible, modular behavior, because you can swap components, replace implementations, and change how things are assembled without altering the external interface of the containing object.

You should prefer composition when you want flexibility and loose coupling. It lets you assemble and reconfigure behavior from interchangeable parts, making it easier to adapt to changing requirements and to test components in isolation. Use inheritance when there’s a clear, stable hierarchy and you want to share a common interface and implementation across many related types, enabling polymorphism.

For example, a Car has an Engine, so it’s composed of components; a Dog is an Animal, so it inherits from Animal to reuse and specialize its behavior. Remember, composition isn’t only about code reuse, and inheritance and composition aren’t the same—each serves different relationship models and design goals.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy