Types of Inheritance

Types of Inheritance

C++ is very flexible in inheritance and provides the different types of inheritance including Single, Multiple, Hierarchical, Multilevel and Hybrid Inheritance.

Different computer languages offer different types of inheritance. However the basic idea remains same that derived class gets access to the (non-private) members of the base class. C++ is very flexible in inheritance and provides the following types of inheritance.

Single Inheritance

This is the basic type of inheritance where the base and derived classes remain one i.e. one class is inherited by exactly one class. It means that there will be only one child and one parent only.

Single Inheritance

As shown above, if a parent class has exactly one child class and the child class has exactly one parent class then this kind of inheritance is called single inheritance.

Multiple Inheritances

In this type of inheritance a child class has more than one parent classes and hence inheriting more than one base class. Advantage of this kind of inheritance is that one child class accesses the members of more than one parent classes thus taking full advantage of inheritance. This is a complex type of inheritance and some computer languages (like Java, C#, VB.Net etc.) do not support this kind of inheritance but as C++ is very flexible in inheritance, so supports multiple inheritances as well. The following figure shows that how it takes place.

Multiple Inheritances

Hierarchical Inheritance

In this type of inheritance, a single parent class has more than one child class. It allows to access the code of one class in more than one derived class. Almost all computer languages supporting object oriented programming support hierarchical inheritance. The following figure depicts this kind of inheritance.

Hierarchical Inheritance

Multilevel Inheritance

As the name suggests, the inheritance can be extended to more than two levels. In this kind of inheritance the same class may be child of the parent class and parent of its child class. In the following figure Class2 is the child of Class1 and at the same time is parent of Class3.

Multilevel Inheritance

Hybrid (Virtual) Inheritance

This kind of inheritance is collection of different types of inheritance discussed above. For example a child class may have more than one parents and a class may have more than one children in multi levels as explained in the figure below.

Hybrid (Virtual) Inheritance

As shown in the figure above, Class1 has more than one child classes and Class5 has more than one parent classes. There is multi-level inheritance too. You can combine any kind of inheritance discussed earlier to form the hybrid inheritance.

More Related Articles For You

    C++ Tutorial

    C++ Quizzes