Types Of Inheritance In C#

Background

In this article we will learn about one of the most reusable object oriented features of C#, inheritance. We will learn about inheritance from the basics because I have written this article focusing on students and beginners. Before proceeding further please refer to my previous articles for a better understanding.

What Inheritance is

Acquiring (taking) the properties of one class into another class is called inheritance. Inheritance provides reusability by allowing us to extend an existing class.  

The reason behind OOP programming is to promote the reusability of code and to reduce complexity in code and it is possible by using inheritance.
 
The following are the types of inheritance in C#.

Types of Inheritance
 
The inheritance concept is based on a base class and derived class. Let us see the definition of a base and derived class.

  • Base class - is the class from which features are to be inherited into another class.
  • Derived class - it is the class in which the base class features are inherited.

Single inheritance 
 
It is the type of inheritance in which there is one base class and one derived class.
Single inheritance
For example,

  1. public class Accountcreditinfo //base class    
  2. {  
  3.     public string Credit()  
  4.     {  
  5.         return "balance is credited";  
  6.     }  
  7. }  
  8. public class debitinfo : Accountcreditinfo //derived class    
  9. {  
  10.     public string debit()  
  11.     {  
  12.         Credit();                       ////derived class method    
  13.         return "balance is debited";  
  14.     }  
  15. }  

In the preceding sample program Accountcreditinfo is the base class and debitinfo is the derived class.

Hierarchical inheritance
 
This is the type of inheritance in which there are multiple classes derived from one base class. This type of inheritance is used when there is a requirement of one class feature that is needed in multiple classes.
 
Hierarchical inheritance

For example,

  1. class A  //base class    
  2. {  
  3.     public string msg()  
  4.     {  
  5.         return "this is A class Method";  
  6.     }  
  7. }  
  8. class B : A  
  9. {  
  10.     public string info()  
  11.     {  
  12.         msg();  
  13.         return "this is B class Method";  
  14.     }  
  15.     class C : A  
  16.     {  
  17.         public string getinfo()  
  18.         {  
  19.             msg();  
  20.             return "this is B class Method";  
  21.         }  
  22.     }  
  23. }    

In the preceding program one base class is derived in many classes hence it is a called a Hierarchical Inheritance.
 
Multilevel inheritance

When one class is derived from another derived class then this type of inheritance is called multilevel inheritance.
 
Multilevel inheritance

For example,

  1. public class Person  
  2. {  
  3.     public string persondet()  
  4.     {  
  5.         return "this is the person class";  
  6.     }  
  7. }  
  8. public class Bird : Person  
  9. {  
  10.     public string birddet()  
  11.     {  
  12.         persondet();  
  13.         return "this is the birddet Class";  
  14.     }  
  15. }  
  16. public class Animal : Bird  
  17. {  
  18.     public string animaldet()  
  19.     {  
  20.         persondet();  
  21.         birddet();  
  22.         return "this is the Animal Class";  
  23.     }  
  24. }   

In the preceding program, each class is derived from one class that is derived from another class hence this type of inheritance is called Multilevel Inheritance.
 
Multiple inheritance using Interfaces
 
C# does not support multiple inheritances of classes. To overcome this problem we can use interfaces, we will see more about interfaces in my next article in detail.
 
Multiple inheritance using Interfaces

For example,

  1. public interface IA //ineterface  1    
  2. {  
  3.     string setImgs(string a);  
  4. }  
  5. public interface IB  //Interface 2    
  6. {  
  7.     int getAmount(int Amt);  
  8. }  
  9. public class ICar : IA, IB //implementatin    
  10. {  
  11.     public int getAmount(int Amt)  
  12.     {  
  13.         return 100;  
  14.     }  
  15.     public string setImgs(string a)  
  16.     {  
  17.         return "this is the car";  
  18.     }  
  19. }    

In the preceding program the ICar class inherits the features of the two interfaces hence this type of inheritance is called Multiple Inheritance.
 
The following are some key points about inheritance:

  1. C# does not support multiple inheritances of classes, the same thing can be done using interfaces.
  2. Private members are not accessed in a derived class when one class is derived from another.

Summary

I hope you now have an overview of inheritance and its types. In the next article we will learn about each inheritance type in detail. If you have any suggestions regarding this article then please contact me.

Up Next
    Ebook Download
    View all
    Learn
    View all