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 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 base class or super class or parent class and the new class created from existing class is called as child class or sub class or derived class. We normally talk in terms of base class and derived class.

 

Basic structure of Inheritance

    class ParentClass
    {
    }
    class ChildClass : ParentClass
    {
    }
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 an 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.

Example of Single Inheritance

    class ParentClass
    {
        public void Run()
        {
            Console.WriteLine("This is parent’s Run method");
        }
        public void Walk()
        {
            Console.WriteLine("This is parent’s Walk method");
        }
    }
    class ChildClass : ParentClass
    {
        public void Eat()
        {
            Console.WriteLine("This is the child’s Eat Method");
        }
    }

 

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

Example of Multilevel Inheritance

    class ParentClass
    {
        public void Run()
        {
            Console.WriteLine("This is parent’s Run method");
        }
        public void Walk()
        {
            Console.WriteLine("This is parent’s Walk method");
        }
    }
    class ChildClass : ParentClass
    {
        public void Eat()
        {
            Console.WriteLine("This is the child’s Eat Method");
        }
    }
    class SecondChildClass : ChildClass
    {
        public void See()
        {
        }
    }

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 inheritances due to the complexity of a code and it creates diamond structure which creates 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.

In short it means single base class having more than one derived classes.

    class ParentClass
    {
        public void Run()
        {
            Console.WriteLine("This is parent’s Run method");
        }
        public void Walk()
        {
            Console.WriteLine("This is parent’s Walk method");
        }
    }
    class ChildClass : ParentClass
    {
        public void Eat()
        {
            Console.WriteLine("This is the child’s Eat Method");
        }
    }
    class SecondChildClass : ParentClass
    {
        public void See()
        {
        }
    }

 

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

It reduces code redundancy.

It provides code reusability.

Reduces source code size and improves code readability.

After using this code is easy to manage and divided into parent and child classes.

It supports code extensibility by overriding the base class functionality within child classes.

 

Disadvantages of Inheritance

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.

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.

 

Conclusion:

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.

I hope this post will help you. Please put your feedback using comment which helps me to improve myself for next post. If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends. Thanks