Skip to content
LLD Learn/OOP Foundations & UML
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Composition vs. Inheritance

4 min read

You'll learn to

  • -Explain composition as "an object holds another object and hands it the work", and inheritance as "is a"
  • -See with numbers why one class per combination explodes while one part per behavior does not
  • -Use a simple smell test to decide when inheritance is still the right tool

Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off. Highlights are saved on this device only. Log in to save them to your account instead.

"Favor composition over inheritance" is one of the most repeated pieces of advice in object-oriented design, and one of the most misapplied. Candidates hear it as "never use inheritance", which is wrong. The real advice is narrower. Use inheritance for a true is-a relationship in a hierarchy that will not change. Use composition for behavior that varies or combines in independent ways.

Composition simply means one object holds another object and hands it some of the work. Handing work over like this is called delegating. Inheritance means a class is a more specific kind of another class.

An Analogy: A Family Tree Versus a Box of Building Bricks

Inheritance is a family tree. Once you are born into it, your parents decide a lot about you, and you cannot change them later. Composition is a box of building bricks. You pick the pieces you need, snap them together and swap one for another whenever the model changes. A family tree is a fine way to describe stable facts, such as a debit card being a kind of payment. It is a poor way to describe behavior that changes.

Where Inheritance Breaks Down

Model ducks with a Duck class that has a fly() method. Then someone adds a RubberDuck, which cannot fly. You override fly() to do nothing, and now every piece of code that calls fly() on a duck must wonder whether it did anything. This is the fragile base class problem: a change or exception in the parent leaks into every child. One family tree cannot express two independent choices, such as flies or does not fly, and quacks or squeaks.

Run the demo to see the composition version. The duck holds a flying behavior and a quacking behavior as separate parts, and you choose each part when you build the duck.

Press Run: no override that does nothing, and no new Duck subclass for the rubber duck

The Arithmetic of a Class Explosion

The real cost shows up when two independent choices multiply. Suppose a notification system has several channels (email, SMS, push, and so on) and each message can be sent in several styles (normal, urgent, digest). With inheritance you write one subclass for every combination, like UrgentEmail. With composition you write one part per channel and one part per style, and combine them when you need to.

Classes to write for channels × styles
3 × 2
6 with inheritance, 5 with composition
5 × 3
15 with inheritance, 8 with composition
10 × 4
40 with inheritance, 14 with composition

Inheritance grows by multiplication (channels × styles) and composition by addition (channels + styles). Adding a sixth channel to the 5 × 3 system costs 3 more classes with inheritance and 1 more with composition.

When Inheritance Is Still the Right Call

Inheritance earns its place when the hierarchy is stable and the is-a statement stays true. A CreditCard and a DebitCard are both a Payment, and how you pay is not a mix of independent choices. The interview signal is not whether you used inheritance. It is whether you noticed that one hierarchy could not capture the variation in the requirements, and switched to composition instead of forcing it.

A practical smell test: if you write an override that does nothing, throws an error, or exists only to cancel a parent's behavior, that is composition's cue to enter. It is not a sign you need a deeper hierarchy.

Interview Signal

You are designing a notification system with Email, SMS, and Push channels, each of which can optionally be "urgent" (different formatting/delivery). How do you avoid an inheritance explosion (EmailUrgent, SMSUrgent, PushUrgent, ...)?

Weak Answer

"I'd create a base Notification class and have each channel/urgency combination extend it as needed."

Strong Answer

"Channel and urgency are two independent axes of variation, so I'd compose them instead: a Notification holds a DeliveryChannel and a UrgencyFormatter as separate objects rather than baking both into one inheritance chain. That way adding a fourth channel or a second urgency level is one new class, not a multiplication of subclasses."

Check Yourself1 / 5

What problem does the classic Duck/RubberDuck example illustrate?

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!