Category: 

What Is Composition over Inheritance?

Article Details
  • Written By: Eugene P.
  • Edited By: Angela B.
  • Last Modified Date: 23 November 2016
  • Copyright Protected:
    2003-2016
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
The great magician and escape artist Harry Houdini died on Halloween in 1926.  more...

December 11 ,  1946 :  UNICEF was established.  more...

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.

Ad

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.

Ad

You might also Like

Recommended

Discuss this Article

Post your comments

Post Anonymously

Login

username
password
forgot password?

Register

username
password
confirm
email