Composition versus inheritance is a choice between "has-a" and "is-a" relationships, and in most Java code you should prefer composition. Inheritance (extends) ties a subclass to the internals of its parent and exposes every inherited method, while composition keeps another object in a field and uses only the methods you choose. Reserve inheritance for true "is-a" cases where the subclass can stand in for the parent everywhere.
This guideline, often written "favour composition over inheritance", comes from the book Effective Java and the Gang of Four design patterns. The sections below show why it holds, with code.
Inheritance models an "is-a" relationship: a Dog is an Animal. Composition models "has-a": a Car has an Engine.
In the composition example, Car exposes only start(). Callers cannot call anything else on the Engine, and you can later change the engine class without breaking code that uses Car.
Inheritance is not wrong, but it has costs that grow with the codebase:
This works with ArrayList today only because its addAll does not call add. If a parent's addAll is implemented by calling add for each element (the well-known HashSet case from Effective Java), the same subclass counts every element twice. The subclass depends on an undocumented detail of the parent, and that detail can change in a later release.
Wrap the list instead of extending it and forward the calls you need.
Now the counting is always correct, because items.addAll is an implementation detail that cannot call back into your class. You also chose which methods to expose.
The strongest form of composition depends on an interface, so the behaviour can be swapped, including in tests.
CheckoutService never inherits from a gateway. You can pass StripeGateway, a fake for tests, or a different provider, without touching the service. This pattern is dependency injection, and it is how Spring and most modern Java code is organised.
Use inheritance when all of these hold:
Shape, Circle, Rectangle.Modern Java also gives you tighter options. A sealed hierarchy limits which classes may extend a type, and a final class forbids extension entirely. If a class was not designed for subclassing, mark it final.
| Question | Prefer inheritance | Prefer composition |
|---|---|---|
| Relationship | Is-a | Has-a or uses-a |
| Need to swap behaviour at runtime | No | Yes |
| Reuse a class you do not control | Risky | Safe |
| Want a small public API | No | Yes |
| Need polymorphism over a family | Yes | Use an interface |
It reduces coupling, hides implementation details, and lets you change behaviour at runtime. A subclass depends on its parent internals, while a composed object depends only on a public interface.
No. Use extends for real is-a relationships where substitution works, such as a class hierarchy designed for polymorphism. Most reuse, though, should come from composition and interfaces.
Delegation is a composed object forwarding a call to the object it holds, as Car.start() forwards to engine.start(). It is the mechanism that makes composition work.
Yes. Implement an interface, hold the shared behaviour in a helper object, and delegate to it. Default methods on interfaces can also share small pieces of behaviour.
Inheritance couples a subclass to its parent and exposes the whole parent API, so the safe default is composition: hold an object in a field, delegate to it, and depend on an interface. Use extends only for genuine is-a relationships that follow the substitution principle, and mark classes final or sealed when they are not meant for open extension.