Compare the Difference Between Similar Terms

Difference Between Inheritance and Composition

Inheritance vs Composition

Inheritance and Composition are two important concepts found in OOP (Object Oriented Programming). In simple terms, both Composition and Inheritance deal with providing additional properties or behavior to a class. Inheritance is the ability for a class to inherit properties and behavior from a parent class by extending it. On the other hand, Composition is the ability of a class to contain objects of different classes as member data.

What is Inheritance?

As mentioned above, Inheritance is the ability for a class to inherit properties and behavior from a parent class by extending it. Inheritance essentially provides code reuse by allowing extending properties and behavior of an existing class by a newly defined class. If class A extends B, then class B is called the parent class (or super class) and class A is called the child class (or derived class/sub class). In this example scenario, class A will inherit all public and protected attributes and methods of the super class (B). The subclass can optionally override (provide new or extended functionality to methods) the behavior inherited from the parent class.

Inheritance represents an “is-a” relationship in OOP. This essentially means that A is also a B. In other words, B can be the class with a general description of a certain real world entity but A specifies a certain specialization. In a real world programming problem, the Person class could be extended to create the Employee class. This is called specialization. But you could also first create the Employee class and then generalize it to a Person class as well (i.e. generalization). In this example, the Employee will have all the properties and behavior of the Person (i.e. Employee is also a Person) and may contain some additional functionality (so, Person is not an Employee) as well.

What is Composition?

Composition is the ability of a class to contain objects of different classes as member data. For example, class A could contain an object of class B as a member. Here, all the public methods (or functions) defined in B can be executed within the class A. Class A becomes the container, while class B becomes the contained class. Composition is also referred to as Containership. In this example, it can be said that class A is composed of class B. In OOP, Composition represents a “has-a” relationship. It is important to note that, even though the container has access to execute all the public methods of the contained class, it is not able to alter or provide additional functionality. When it comes to a real world programming problem, an object of class TextBox may be contained in the class Form, and thus can be said that a Form contains a TextBox (or alternatively, a Form is composed of a TextBox).

What is the difference between Inheritance and Composition?

Although Inheritance and Composition are two OOP concepts, they are quite different in what they allow the programmer to achieve. Inheritance is the ability for a class to inherit properties and behavior from a parent class by extending it, while Composition is the ability of a class to contain objects of different classes as member data. If a class is extended, it inherits all the public and protected properties/behavior and those behaviors may be overridden by the subclass. But if a class is contained in another, the container does not get the ability to change or add behavior to the contained. Inheritance represents an “is-a” relationship in OOP, while Composition represents a “has-a” relationship.