Which technique best facilitates code reuse without rigid inheritance?

Get ready for your Object‑Oriented Programming Test. Use flashcards and multiple-choice questions. Each question includes hints and explanations. Prepare for your exam today!

Multiple Choice

Which technique best facilitates code reuse without rigid inheritance?

Explanation:
Design for reuse by combining objects rather than extending a single class. Inheritance creates an is-a relationship that pulls in code from a base class and couples all subclasses to that hierarchy. Change in the base can ripple through many derived classes, and you’re often forced into a single tree of types, which makes it hard to mix and match behaviors. Composition over inheritance avoids that rigidity by letting objects include other objects that implement specific capabilities and delegate work to them. You can assemble different capabilities into new combinations without rewriting class hierarchies, and you can swap components to alter behavior at runtime. This promotes reuse because the same reusable components (like a logger, a validator, or a rendering strategy) can be plugged into many different classes. Context helps: use composition to share functionality across disparate classes by holding and delegating to component objects. Patterns such as Strategy (swap algorithms), Decorator (add responsibilities by wrapping), or simple delegation illustrate how you reuse code through composition rather than inheritance. The other ideas don’t fit as well here. Abstraction without implementation defines a contract but doesn’t provide reusable code paths. Encapsulation focuses on hiding internal state and protecting invariants, not on assembling reusable behavior. Inheritance over composition tends to create rigid hierarchies, making it harder to combine independent capabilities without creating fragile dependencies.

Design for reuse by combining objects rather than extending a single class. Inheritance creates an is-a relationship that pulls in code from a base class and couples all subclasses to that hierarchy. Change in the base can ripple through many derived classes, and you’re often forced into a single tree of types, which makes it hard to mix and match behaviors.

Composition over inheritance avoids that rigidity by letting objects include other objects that implement specific capabilities and delegate work to them. You can assemble different capabilities into new combinations without rewriting class hierarchies, and you can swap components to alter behavior at runtime. This promotes reuse because the same reusable components (like a logger, a validator, or a rendering strategy) can be plugged into many different classes.

Context helps: use composition to share functionality across disparate classes by holding and delegating to component objects. Patterns such as Strategy (swap algorithms), Decorator (add responsibilities by wrapping), or simple delegation illustrate how you reuse code through composition rather than inheritance.

The other ideas don’t fit as well here. Abstraction without implementation defines a contract but doesn’t provide reusable code paths. Encapsulation focuses on hiding internal state and protecting invariants, not on assembling reusable behavior. Inheritance over composition tends to create rigid hierarchies, making it harder to combine independent capabilities without creating fragile dependencies.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy