DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

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.

Joel Quenneville user avatar by
Joel Quenneville
·
Nov. 05, 16 · Opinion
Like (13)
Save
Tweet
Share
13.25K Views

Join the DZone community and get the full member experience.

Join For Free
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.

Conclusion

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.

Inheritance (object-oriented programming)

Published at DZone with permission of Joel Quenneville, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top Five Tools for AI-based Test Automation
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • How to Create a Real-Time Scalable Streaming App Using Apache NiFi, Apache Pulsar, and Apache Flink SQL
  • The Real Democratization of AI, and Why It Has to Be Closely Monitored

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: