Different types of Inheritance in OOPs with Example

Different types of Inheritance in OOPs with Example

Purpose of this session is to share importance of Inheritance. We will discuss about C++ inheritance in short. Also we will discuss the importance different types of inheritance with Example.

What is Inheritance?

Process of inheritance is, “Object of one class can acquire the properties of another class.”. Lets take an example I have a base class “birds”. It has some properties & methods. If I want to distinguish between flying & non-flying birds then these classes can be derived from birds class (As the base class). Because common methods & properties for flying birds & non-flying birds must be there is birds class.

Fig: Example of Inheritance

Types of Inheritance

There are 5 types of Inheritance available in Object Oriented Programming (OPPs), Those are as below:

  • Single Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

1. Single Inheritance

Single inheritance is very much easy to understand. One class that is derived class extends another class or base class this mechanism is Single inheritance.

Fig: Single Inheritance

Refer to the above example think you have a base class Office. While Creating an Employee Class for Office related properties you can get in Office class. Here two things comes into picture base class & derived class. This scenario is called Single Inheritance.

2. Multilevel Inheritance

One derived class extends from another derived class this mechanism is known as Multilevel Inheritance.

Fig: Multilevel Inheritance

Sometime it happens a derived class brings property from parent of parent class. Lets talk about your Grand Father here your parents are the derived class for your grand father & you are the derived class for your parents. But you have some properties of your Grand Father.

3. Multiple Inheritance

In Multiple Inheritance, there are several base classes with single derived class. The issue with this inheritance is that more than one base class will have to manage by single derived class.

Fig: Multiple Inheritance

In the software projects, Multiple Inheritance is very rarely used. C++ supported the Multiple Inheritance.

4. Hierarchical Inheritance

In Hierarchical Inheritance, one class is inherited by more than one class.

Fig: Hierarchical Inheritance

5. Hybrid Inheritance

Combination of Single Inheritance and Multiple Inheritance are called as Hybrid Inheritance. More than one type of inheritance is used in the different design.

Fig: Hybrid Inheritance

Advantages of Inheritance

  • One of the most important advantage of inheritance is “Re-usability”. Don’t need to write similar classes for several instances.
  • Data hiding mechanism. Functionality Oriented rather rotate around Codes.
  • Inheritance makes application code more flexible to change or update.
  • Overriding – We can able to override the base class methods.