Object‑Oriented Programming (OOP) Practice Test

Session length

1 / 20

Which technique best facilitates code reuse without rigid inheritance?

Composition over inheritance

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.

Inheritance over composition

Abstraction without implementation

Encapsulation only

Next question

Find the option that is right for you!

All options are one-time payments.

$12.50

30 day premium pass

All the basics to get you started

  • Ad-free experience
  • View your previous attempt history
  • Mobile app access
  • In-depth explanations
  • 30 day premium pass access
👑$30.00 $87.50 usd

6 month DELUXE pass (most popular)

Everything with the 30 day premium pass FOR 6 MONTHS! & the ultimate digital PDF study guide (BONUS)

  • Everything included in the premium pass
  • $87.50 usd value for $30.00! You save $57.50!
  • + Access to the ultimate digital PDF study guide
  • + 6 months of premium pass access
  • + Priority support
$12.50 $18.99

Ultimate digital PDF study guide

For those that prefer a more traditional form of learning

  • Available for instant download
  • Available offline
  • Hundreds of practice multiple choice questions
  • Comprehensive content
  • Detailed explanations
Image Description
Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy