What Is Composition over Inheritance?

Composition over inheritance is a design principle that favors combining simple objects to create complex functionalities rather than relying on a strict parent-child hierarchy. This approach enhances flexibility and reusability in programming. It's like using building blocks to construct a versatile structure. Intrigued by how this can revolutionize your coding practices? Let's examine its impact on software development together.
Eugene P.
Eugene P.

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.

Man holding computer
Man holding computer

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.

You might also Like

Discuss this Article

Post your comments
Forgot password?
    • Man holding computer
      Man holding computer