Please enable Javascript to correctly display the contents on Dot Net Tricks!

Understanding Inheritance and Different Types of Inheritance

  Author : Shailendra Chauhan
Updated On : 26 Sep 2016
Total Views : 67,347   
 

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.

Advantages

  1. Reduce code redundancy.

  2. Provides code reusability.

  3. Reduces source code size and improves code readability.

  4. 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.

Disadvantages

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

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

Different Types of Inheritance

OOPs supports the six types of inheritance as given below-

  1. Single inheritance

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

    //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.

    //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.

    //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 another derived classes and the same base class of another derived classes. This inheritance is not supported by .NET Languages like C#, F# etc.

    //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 classes are created from a single base.

    //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 combination of more than one inheritance. Hence, it may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or Hierarchical and Multipath inheritance or Hierarchical, Multilevel and Multiple inheritance.

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

    //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:
     }
    }
    
What do you think?

I hope you will enjoy the tips while programming with .NET Languages like C#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.


YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (1)
Reena Ameta Reply

Really this blog very helpful for everyone who’s want to self learn.

13 FEB
ASP.NET Core Development (online)

07:00 AM-09:00 AM IST( MON, WED, FRI)

Know More
25 JAN
Angular2 and Angular1 Development (online)

09:00 PM-10:30 PM IST(MON-FRI)

Know More
24 JAN
ASP.NET MVC with Angular2 Development (online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

Know More
21 JAN
Xamarin Forms : Build Cross-platform Apps (offline)

05:00 PM-06:30 PM IST

Know More
20 JAN
NODEJS & MEAN Stack 2 Development (online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

Know More
15 JAN
PPC Marketing (offline)

04:00 PM-05:300 PM

10 JAN
ASP.NET MVC with AngularJS Development (online)

09:00 PM-11:00 PM IST on (TUE, THRU,SAT)

3 JAN
ASP.NET MVC with AngularJS Development (online)

07:00 AM-09:00 AM IST

31 DEC
ASP.NET MVC with AngularJS Development (offline)

09:30 AM-11:00 AM

31 DEC
Angular2 and Angular1 Development (offline)

11:00 AM-12:30 AM

26 NOV
ASP.NET MVC with AngularJS Development (offline)

03:00 PM-04:30 PM

5 NOV
ASP.NET MVC with AngularJS Development (offline)

08:00 AM-09:30 AM

4 SEP
MEAN Stack 2 Development (offline)

11:00 AM-12:30 PM.

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+