Understanding Inheritance and Different Types of Inheritance

05 Sep 2022
Intermediate
808K Views

Inheritance is a mechanism of acquiring the features and behaviors of a class by another class. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. Inheritance implements the IS-A relationship.

For example, mammal IS-A animal, dog IS-A mammal; Hence dog IS-A animal as well.

Inheritance is one of the core aspects of the fundamental called Object-Oriented Programming (OOPs) and if we need to describe then inheritance is that it provides the way of achieving code re-usability were writing the same code the multiple times, again and again, rather we can use inherit a version of the given properties of one class into the other by extending it. We will learn each type of inheritance in this article

Different Types of Inheritance

OOPs support the six different types of inheritance as given below :

  1. Single inheritance

  2. Multi-level inheritance

  3. Multiple inheritance

  4. Multipath inheritance

  5. Hierarchical Inheritance

  6. Hybrid Inheritance

Types of Inheritance
  1. Single inheritance

    In this inheritance, a derived class is created from a single base class.

    In the given example, Class A is the parent class and Class B is the child class since Class B inherits the features and behavior of the parent class A.

    Single Inheritance

    The syntax for Single Inheritance

    //Base Class
    class A 
    {
     public void fooA()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class B : A
    {
     public void fooB()
     {
     //TO DO:
     }
    }
    
  2. Multi-level inheritance

    In this inheritance, a derived class is created from another derived class.

    In the given example, class c inherits the properties and behavior of class B and class B inherits the properties and behavior of class B. So, here A is the parent class of B and class B is the parent class of C. So, here class C implicitly inherits the properties and behavior of class A along with Class B i.e there is a multilevel of inheritance.

    Multi-level inheritance

    The syntax for Multi-level Inheritance

    //Base Class
    class A 
    {
     public void fooA()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class B : A
    {
     public void fooB()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class C : B
    {
     public void fooC()
     {
     //TO DO:
     }
    }
    
  3. Multiple inheritance

    In this inheritance, a derived class is created from more than one base class. This inheritance is not supported by .NET Languages like C#, F#, etc., and Java Language.

    In the given example, class c inherits the properties and behavior of class B and class A at the same level. So, here A and Class B both are the parent classes for Class C.

    Multiple inheritance

    The syntax for Multiple Inheritance

    //Base Class
    class A 
    {
     public void fooA()
     {
     //TO DO:
     }
    }
    
    //Base Class
    class B
    {
     public void fooB()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class C : A, B
    {
     public void fooC()
     {
     //TO DO:
     }
    }
    
  4. Multipath inheritance

    In this inheritance, a derived class is created from other derived classes and the same base class of other derived classes. This inheritance is not supported by .NET Languages like C#, F#, etc.

    In the given example, class D inherits the properties and behavior of class C and class B as well as Class A. Both class C and class B inherit the Class A. So, Class A is the parent for Class B and Class C as well as Class D. So it's making it a Multipath inheritance.

    Multipath inheritance

    The syntax for Multipath Inheritance

    //Base Class
    class A
    {
     public void fooA()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class B : A
    {
     public void fooB()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class C : A
    {
     public void fooC()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class D : B, A, C
    {
     public void fooD()
     {
     //TO DO:
     }
    }
    
  5. Hierarchical Inheritance

    In this inheritance, more than one derived class is created from a single base class and further child classes act as parent classes for more than one child class.

    In the given example, class A has two children class B and class D. Further, class B and class C both are having two children - class D and E; class F and G respectively.

    Hierarchical Inheritance

    The syntax for Hierarchical Inheritance

    //Base Class
    class A
    {
     public void fooA()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class B : A
    {
     public void fooB()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class C : A
    {
     public void fooC()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class D : C
    {
     public void fooD()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class E : C
    {
     public void fooE()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class F : B
    {
     public void fooF()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class G :B
    {
     public void fooG()
     {
     //TO DO:
     }
    }
    
  6. Hybrid inheritance

    This is a combination of more than one inheritance. Hence, it may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance Hierarchical and Multipath inheritance, or Hierarchical, Multilevel and Multiple inheritances.

    Since .NET Languages like C#, F#, etc. do not support multiple and multipath inheritance. Hence hybrid inheritance with a combination of multiple or multipath inheritances is not supported by .NET Languages.

    The syntax for Hybrid Inheritance

    //Base Class
    class A
    {
     public void fooA()
     {
     //TO DO:
     }
    }
    
    //Base Class
    class F
    {
     public void fooF()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class B : A, F
    {
     public void fooB()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class C : A
    {
     public void fooC()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class D : C
    {
     public void fooD()
     {
     //TO DO:
     }
    }
    
    //Derived Class
    class E : C
    {
     public void fooE()
     {
     //TO DO:
     }
    }
    

Advantages of Inheritance

  1. Reduce code redundancy.

  2. Provides better code reusabilities.

  3. Reduces source code size and improves code readability.

  4. The code is easy to manage and divided into parent and child classes.

  5. Supports code extensibility by overriding the base class functionality within child classes.

    Code usability will enhance the reliability eventually where the base class code will always be tested and debugged against the issues.

Disadvantages of Inheritance

  1. In Inheritance base class and child class, both are tightly coupled. Hence If you change the code of the parent class, it will affect all the child classes.

  2. In a class hierarchy, many data members remain unused and the memory allocated to them is not utilized. Hence it affects the performance of your program if you have not implemented inheritance correctly.

    Inheritance increases the coupling between the base class and the derived class. any small change in the base class will directly affect all the child classes which are extended to the parent class.

Read More Articles Related to Object-oriented Programming Language
What do you think?

In this article, you learned about the different types of inheritance including advantages and disadvantages. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Learn to Crack Your Technical Interview

Accept cookies & close this