Spiegato, Home

Technology and Gadgets

What Is Composition over Inheritance?

  • Published by Spiegato
  • 2 min read

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.

Key terms in this article

Related articles

  • What is Interface Inheritance?

    Interface inheritance is a mechanism in certain computer languages where specific functions are inherited by a class, and then that particular class must…

  • What is Protected Inheritance?

    The field of computer science uses programming to create solutions to problems. C++ is a programming language used in computer science that utilizes…

  • What does “Multiple Inheritance” Mean?

    Multiple inheritance, when speaking of object-oriented programming (OOP) languages, is a term that refers to a class that inherits functionality from more…

  • What is Virtual Inheritance?

    Virtual inheritance is a type of inheritance in which the implementation of a superclass is incomplete, and a subclass is required for complete definition…

  • What Is Single Table Inheritance?

    Single table inheritance is a coding method in which object-oriented inheritance, which usually is not included on a database program, is emulated for a…