Compare composition and inheritance as design strategies and when to prefer composition?

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

Compare composition and inheritance as design strategies and when to prefer composition?

Explanation:
The main idea tested here is how composition and inheritance influence flexibility and coupling in object-oriented design. Composition builds behavior by containing and delegating to other objects (has-a relationships), which lets you swap or change components without altering the object’s type. This delegation-based approach reduces tight coupling and makes the system more adaptable, because you can replace a component with another that satisfies the same interface, even at runtime in some designs. That flexibility is why composition tends to support easier extension and maintenance. In contrast, inheritance creates an is-a relationship and often leads to fixed class hierarchies. Changes in a base class can ripple through many subclasses, making the design harder to evolve and more fragile. Because of that, inheritance can reduce flexibility and increase coupling when the hierarchy becomes deep or rigid. So, composition is typically preferred when you want code reuse through delegation without tying classes to a rigid inheritance structure. Also, note that composition does not create an is-a relationship—it’s a has-a relationship. And the idea that inheritance is always best for code reuse ignores the downsides of tight coupling and fragile hierarchies, which composition helps avoid by keeping components interchangeable through well-defined interfaces.

The main idea tested here is how composition and inheritance influence flexibility and coupling in object-oriented design. Composition builds behavior by containing and delegating to other objects (has-a relationships), which lets you swap or change components without altering the object’s type. This delegation-based approach reduces tight coupling and makes the system more adaptable, because you can replace a component with another that satisfies the same interface, even at runtime in some designs. That flexibility is why composition tends to support easier extension and maintenance.

In contrast, inheritance creates an is-a relationship and often leads to fixed class hierarchies. Changes in a base class can ripple through many subclasses, making the design harder to evolve and more fragile. Because of that, inheritance can reduce flexibility and increase coupling when the hierarchy becomes deep or rigid. So, composition is typically preferred when you want code reuse through delegation without tying classes to a rigid inheritance structure.

Also, note that composition does not create an is-a relationship—it’s a has-a relationship. And the idea that inheritance is always best for code reuse ignores the downsides of tight coupling and fragile hierarchies, which composition helps avoid by keeping components interchangeable through well-defined interfaces.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy