We are independent & ad-supported. We may earn a commission for purchases made through our links.
Advertiser Disclosure
Our website is an independent, advertising-supported platform. We provide our content free of charge to our readers, and to keep it that way, we rely on revenue generated through advertisements and affiliate partnerships. This means that when you click on certain links on our site and make a purchase, we may earn a commission. Learn more.
How We Make Money
We sustain our operations through affiliate commissions and advertising. If you click on an affiliate link and make a purchase, we may receive a commission from the merchant at no additional cost to you. We also display advertisements on our website, which help generate revenue to support our work and keep our content free for readers. Our editorial team operates independently of our advertising and affiliate partnerships to ensure that our content remains unbiased and focused on providing you with the best information and recommendations based on thorough research and honest evaluations. To remain transparent, we’ve provided a list of our current affiliate partners here.
Software

Our Promise to you

Founded in 2002, our company has been a trusted resource for readers seeking informative and engaging content. Our dedication to quality remains unwavering—and will never change. We follow a strict editorial policy, ensuring that our content is authored by highly qualified professionals and edited by subject matter experts. This guarantees that everything we publish is objective, accurate, and trustworthy.

Over the years, we've refined our approach to cover a wide range of topics, providing readers with reliable and practical advice to enhance their knowledge and skills. That's why millions of readers turn to us each year. Join us in celebrating the joy of learning, guided by standards you can trust.

What Is Composition over Inheritance?

By Eugene P.
Updated: May 16, 2024

Composition over inheritance is a programming methodology that is used in object oriented programming languages to help establish more dynamic and changeable relationships between objects in an application. The basic format for composition over inheritance is the inclusion of an instance of an object inside another object. This is distinctly different from the object oriented method of inheritance but can serve the same purpose in many models. The advantages of composition over inheritance include well-defined borders between the composited objects and the wrapper objects, the ability to change the implementation of an object without affecting an entire inheritance hierarchy, and a less restrictive framework for initial program design. Some of the complications, however, could include difficulty in serialization and overly complex structures in large, established programs.

In object oriented programming, one of the traits that is important to establish during design is how the different objects relate to one another. Inheritance is a central mechanism in object oriented programming and allows one object to inherit all the data and methods of its parent, which it can then add to, modify or extend with its own implementation. An example of design based on inheritance could be with a program that has one main class that is defines a car and models some generic attributes of all cars. The class for each individual brand of car is then made as a descendant of the main class but defines specific information, such as the engine size, fuel capacity or other information specific to that particular brand of car.

Composition is expressed by including object instances within other objects. If there is a main class that defines a generic car, then instead of creating subclasses for specific car brands, the main class could instead contain instances of other objects that define the car more specifically. This could mean the main car class could contain an object for the engine type and one for the fuel capacity, which would be set when the class is used to create an instance of a specific brand of car.

With composition over inheritance, the objects that are contained in the main class are not directly related to the main class, giving developers the freedom to change how individual objects work. An issue that composition over inheritance can help to prevent is a situation in which an inheritance tree can be excessively deep, meaning that any modifications to a class in the tree could require changes down the length of the tree. Not all object oriented languages have support for multiple inheritances, and composition over inheritance is a method that can be used to work around this restriction.

EasyTechJunkie is dedicated to providing accurate and trustworthy information. We carefully select reputable sources and employ a rigorous fact-checking process to maintain the highest standards. To learn more about our commitment to accuracy, read our editorial process.
Discussion Comments
Share
EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.

EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.