All About Inheritance in C#

Inheritance means acquiring the features and behaviors of a class by another class. It is a parent-child relationship. Using Inheritance methodology you can create a new class by using the existing class. It also referred to as reusability of the code. So when you use Inheritance you can reuse the code again and again.

In Inheritance, Main class is called the base class or super class orparent class and the new class created from existing class are called as child class or sub class, child class, and derived class. We normally talk in terms of base class and derived class.

Basic structure of Inheritance

  1. class ParentClass  
  2. {  
  3.   
  4. }  
  5. class ChildClass : ParentClass  
  6. {  
  7. }  
Why we use inheritance?

It is used when we create a class and we want to reuse some of the methods or properties from existing class then that is a right way to implement inheritance.

Types of inheritance

There are many types of Inheritance. I will explain all of them one by one.

Single Inheritance

Single Inheritance means there is a single base class which is implemented by single derived class is called as Single Inheritance. Here only one parent class and one derived class.

Single Inheritance

Example of Single Inheritance
  1. class ParentClass  
  2. {  
  3.     public void Run()  
  4.     {  
  5.         Console.WriteLine("This is parent’s Run method");  
  6.     }  
  7.     public void Walk()  
  8.     {  
  9.         Console.WriteLine("This is parent’s Walk method");  
  10.     }  
  11. }  
  12. class ChildClass : ParentClass  
  13. {  
  14.     public void Eat()  
  15.     {  
  16.         Console.WriteLine("This is the child’s Eat Method");  
  17.     }  
  18. }  
Here I am using single inheritance using single base class.

Multilevel Inheritance

When you create a derived class which inherited from another derived class or in simple word if a class is created by using another derived class and this type of implementation is called as multilevel Inheritance

Multilevel Inheritance

Example of Multilevel Inheritance
  1. class ParentClass  
  2. {  
  3.     public void Run()  
  4.     {  
  5.         Console.WriteLine("This is parent’s Run method");  
  6.     }  
  7.     public void Walk()  
  8.     {  
  9.         Console.WriteLine("This is parent’s Walk method");  
  10.     }  
  11. }  
  12. class ChildClass : ParentClass  
  13. {  
  14.     public void Eat()  
  15.     {  
  16.         Console.WriteLine("This is the child’s Eat Method");  
  17.     }  
  18. }  
  19. class SecondChildClass : ChildClass  
  20. {  
  21.     public void See()  
  22.     {  
  23.     }  
  24. }  
From the above source code, you can see that we have achieved multilevel Inheritance by implementing one derived class to another derived class.

Multiple Inheritance

C# does not support multiple inheritance due to the complexity of a code and it creates the diamond structure which create ambiguity.

Hierarchical Inheritance

When we create a structure of project as like that where more than one derived classes are implemented from a same parent class or base class then that type of implantation is known as hierarchical inheritance.

Hierarchical Inheritance

In short it means single base class having more than one derived classes.
  1. class ParentClass  
  2. {  
  3.     public void Run()  
  4.     {  
  5.         Console.WriteLine("This is parent’s Run method");  
  6.     }  
  7.     public void Walk()  
  8.     {  
  9.         Console.WriteLine("This is parent’s Walk method");  
  10.     }  
  11. }  
  12. class ChildClass : ParentClass  
  13. {  
  14.     public void Eat()  
  15.     {  
  16.         Console.WriteLine("This is the child’s Eat Method");  
  17.     }  
  18. }  
  19. class SecondChildClass : ParentClass  
  20. {  
  21.     public void See()  
  22.     {  
  23.     }  
  24. }  
Here you can see that above the code that base class "BaseClass" and two derived classes "childClass" and "SecondChildClass" which are implemented from same base class i.e. "BaseClass".

As you see that we have two derived classes implemented from same base class. This type of implementation can also be called as Hierarchical Inheritance.

Advantages of Inheritance

 

  1. It reduces code redundancy.
  2. It provides code reusability.
  3. Reduces source code size and improves code readability.
  4. After using this code is easy to manage and divided into parent and child classes.
  5. It supports code extensibility by overriding the base class functionality within child classes.

Disadvantages of Inheritance

  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 the class hierarchy, many data members remain unused and the memory allocated to them is not utilized. Hence affect the performance of your program if you have not implemented inheritance correctly.

So, finally in this article you see that what is inheritance and why we use inheritance in C# also you go through about types of inheritance.