DSA Factory
Free Object-oriented basics lessonsObject-oriented basics · Stage 3 · Abstraction and design choices · Step 2

Composition over inheritance

Holding another object and delegating to it, instead of extending it, and when each is the better design. About 12 minutes.

Quick check

Which is the strongest reason to prefer composition for reuse?

  1. AThe class depends only on the other class's public behaviour, and the collaborator can be swapped
  2. BIt always needs less code
  3. CIt is faster
Show the answer

The class depends only on the other class's public behaviour, and the collaborator can be swapped. Looser coupling and run-time flexibility.

Lock it in

4 questions on this idea

Short questions that apply the idea to new cases: trace it, work out a number, put steps in order. A wrong answer explains itself.

Answer them in your browserFree account, no card. Your progress is saved, and the idea comes back in a quick review later.
Sign up free to solve