Over a million developers have joined DZone.

A Look at Composition vs. Inheritance

DZone's Guide to

A Look at Composition vs. Inheritance

When does it make sense to use inheritance? The old adage says to prefer composition over inheritance, but every rule has exceptions.

· Java Zone ·
Free Resource

Start coding something amazing with our library of open source Cloud code patterns. Content provided by IBM.

Designing object-oriented software is hard, and designing reusable object-oriented software is even harder.

This is the opening line of the classic manual on software design Design Patterns: Elements of Reusable Object-Oriented Software, published in 1994.

More than 20 years later, this statement still rings true. The first chapter of the book introduces a few high-level principles to accomplish this goal. One of them stands out:

Prefer composition over inheritance

What advantages does composition have over inheritance?

Coupling and Cohesion

Ideally, the objects in our system have high cohesion (i.e. a single responsibility) and low coupling with other objects.

Composition-based solutions break up a problem into distinct responsibilities and encapsulate the implementation of each into separate objects. In an inheritance-based solution, a given object often has multiple responsibilities it has inherited from its various ancestors.

Since complex functionality is assembled by combining a series of objects that can only communicate via their public interface, they tend to rely more strongly on polymorphism and duck typing than the equivalent inheritance-based implementations. This lines up with another principle described in the chapter:

Code to an interface, not an implementation

This lowers coupling and allows you to handle special cases by passing in an object with a different implementation but the same interface.

Runtime Dependencies

In a composition-based system, the various components get combined together at run-time to create the desired behavior. This allows flexibility to build behavior dynamically without having to resort to clever metaprogramming tricks.

Using Inheritance

Composition has a few downsides, too. It tends to add more indirection to a system. It can also be more work to assemble all the components together into the desired behavior. Anything that can be implemented via inheritance can alternatively be implemented using composition. So when should you use inheritance?

Use inheritance when the cost of indirection and construction of a composition-based approach is higher than the benefits of encapsulated responsibilities and flexibility.

Use inheritance when your object already has a single responsibility and you really do just want specializations.

Use inheritance when you don’t have a choice such as when building ActiveRecord models.


In general, composition is a more robust and flexible approach to architect reusable object-oriented software. Composition should be your go-to solution most of the time. Only reach for inheritance when it makes sense.

Further Reading

  • The first chapter of Design Patterns: Elements of Reusable Object-Oriented Software by Gamma, Helm, Johnson, and Vlissides has a great discussion on the concepts behind what makes software components easy to change and reuse.
  • The first chapter of Design Patterns in Ruby by Russ Olson has a similarly good analysis, specifically for Ruby.

In both cases, it’s worth reading that first chapter even if you don’t read the whole book.

See the original article here.

Use this tool to look at the contents of GitHub and classify code based on the programming language used.  Content provided by IBM Developer.

object oriented programming ,composition ,inheritance ,java

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}