What is the Difference Between Inheritance and Composition

March 2023 · 4 minute read

The main difference between Inheritance and Composition is that inheritance allows using properties and methods of an existing class in the new class while composition is a special type of aggregation that describes the ownership between the associated objects.

Object Oriented Programming (OOP) is a major paradigm used in software development. It allows programmers to model the software using a set of objects. An object is an entity that has state and behaviors. Developers create objects using a class. Therefore, a class is a blueprint.  Moreover, a class contains properties and methods. The properties represent the state of an object whereas the methods denote the behaviors of an object. Furthermore, Inheritance and Composition are two concepts released to OOP.

Key Areas Covered

1. What is Inheritance
     – Definition, Functionality
2. What is Aggregation
     – Definition, Functionality
3. What is Composition
     – Definition, Functionality
4. Difference Between Inheritance and Composition
    – Comparison of Key Differences

Key Terms

Aggregation, Class, Composition, Inheritance, Methods, Objects, OOP

Difference Between Inheritance and Composition - Comparison Summary

What is Inheritance

Inheritance refers to using properties and methods of an already exiting class in a new class. We call this already existing class the parent class, superclass or base class. The new class is called the child class, sub class or derived class. The developer does not have to write code from the very beginning because of inheritance. He can use the properties and methods of an existing class in the new class using inheritance. Therefore, inheritance increases code reusability.

Difference Between Inheritance and Composition

There are various types of inheritance.

Single level Inheritance – For examples, assume that there are two classes: A and B. Class B inherits from class A. Then, it is a single level inheritance.

Multi-level inheritance – This inheritance type has an intermediate class. For instance, assume that there are three classes: A, B, and C. Class C inherits from class B while Class B inherits from class A. 

Multiple Inheritance –  To understand this concept, assume that there are three classes as A, B, and C. Class C inherits from both class A and B. C is the subclass while A and B are superclasses. 

Hierarchical Inheritance – When there are three classes as A, B, and C. Class B and C inherits from class A. Therefore, class A is the superclass and class B and C are the subclasses. 

Hybrid Inheritance – This is a combination of multi-level and multiple inheritances. When A, B, C, and D are classes, B and C inherits from A while D inherits from B and C. 

What is Aggregation

It is important to understand aggregation before understanding composition. In OOP, the objects communicate with each other using methods. Aggregation is an association that describes the “has a” relationship between objects. There is no dependency between the two objects. For instance, assume a classroom and student. Student object can exist without the classroom object. So, this is an aggregation. In other words, if the part can exist outside the whole, we call this an aggregation.

What is Composition

Composition is a special type of aggregation that implies ownership. An object depends on another object and there is a dependency. Let’s look at a school object and a classroom object. The classroom object cannot exist without the school object. If the school object is destroyed, the classroom object is also destroyed. Thus, there is a dependency between these two objects. So, this is a composition. In other words, if the part cannot exist outside the whole, we call it a composition.

Difference Between Inheritance and Composition

Definition

Inheritance is the methodology of creating a new class using the properties and methods of an existing class while composition is a special type of aggregation that implies ownership. Thus, this is the main difference between Inheritance and Composition.

Functionality

Moreover, inheritance allows using the already existing code. However, in composition, destroying the owning object affects the containing object.

Usage

Another difference between Inheritance and Composition is that inheritance provides code reusability while composition allows representing associations.

Conclusion

Inheritance and Composition are concepts related to OOP. The main difference between inheritance and composition is that inheritance allows using properties and methods of an existing class in the new class while composition is a special type of aggregation that describes the ownership between the associated objects.

References:

1.career, People. 6 5 Using Aggregation and Composition, People Career, 22 May 2015, Available here.
2.”Inheritance in Java.” Www.javatpoint.com, Available here.

Image Courtesy:

1.”Inheritance Diagram” By Pluke – Own work (CC0) via Commons Wikimedia

ncG1vNJzZmiolZm2oq2NnKamZ6edrrV5yKxkraCVYrGqssWeqZ6mk5p6o7HTsJyepl2eu6mx0aKrmqaTmnqiusNmmqiloKTAqsDIqKVo