Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What Is Composition vs Inheritance in Java and Which Should You Prefer?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 30, 2026
6 min read

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.

Table of Contents

  • Is-a versus has-a
  • The problems with inheritance
    • A classic example of breakage
  • Rewriting it with composition
  • Composition with interfaces for flexibility
  • When inheritance is the right tool
  • Frequently Asked Questions
    • Why is composition preferred over inheritance?
    • Does composition mean never using extends?
    • What is delegation?
    • Can I get both reuse and polymorphism without inheritance?
  • Summary
  • Related Articles

Is-a versus has-a

Inheritance models an "is-a" relationship: a Dog is an Animal. Composition models "has-a": a Car has an Engine.

java

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.

The problems with inheritance

Inheritance is not wrong, but it has costs that grow with the codebase:

  1. Tight coupling. A subclass depends on how the parent is implemented, not just on what it promises. A change in the parent can silently break children (the fragile base class problem).
  2. Exposed API. Every public method of the parent becomes part of the subclass API, even those that make no sense for it.
  3. Single inheritance. A Java class can extend only one class, so you spend your only inheritance slot on code reuse.
  4. Fixed at compile time. You cannot switch the parent at runtime.

A classic example of breakage

java

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.

Rewriting it with composition

Wrap the list instead of extending it and forward the calls you need.

java

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.

Composition with interfaces for flexibility

The strongest form of composition depends on an interface, so the behaviour can be swapped, including in tests.

java

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.

When inheritance is the right tool

Use inheritance when all of these hold:

  • The relationship is truly is-a, and the Liskov substitution principle holds: code written for the parent works with any subclass.
  • The parent was designed and documented for extension, or you control both classes.
  • You want polymorphism over a family of types, for example 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.

QuestionPrefer inheritancePrefer composition
RelationshipIs-aHas-a or uses-a
Need to swap behaviour at runtimeNoYes
Reuse a class you do not controlRiskySafe
Want a small public APINoYes
Need polymorphism over a familyYesUse an interface

Frequently Asked Questions

Why is composition preferred over inheritance?

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.

Does composition mean never using extends?

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.

What is delegation?

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.

Can I get both reuse and polymorphism without inheritance?

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.

Summary

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.

Related Articles

  • What Is the equals and hashCode Contract in Java?
  • Association vs Aggregation vs Composition in Java Explained
  • Learn Java: Complete Study Roadmap from Beginner to Advanced
Share

Similar posts

  • What Is the equals and hashCode Contract in Java?
  • What Is the Object Class in Java? equals, hashCode and toString
  • What Is Method Overriding in Java and How Is It Different from Overloading?
  • What Is Polymorphism in Java? Compile-Time vs Runtime
  • What Is Inheritance in Java and How Does extends Work?
  • What Is Encapsulation in Java and How Do Getters and Setters Help?
  • What Are Access Modifiers in Java: public, private, protected, default?
  • What Is the static Keyword in Java? Variables, Methods, Blocks