Learn All About Inheritance in C#

Introduction

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

In Inheritance, the Main class is called the base class, superclass, or parent class, and the new class created from the existing class is called the child class or subclass and derived class. We normally talk in terms of the base class and derived class.

The basic structure of Inheritance in C#

Here is the basic structure of inheritance. In this below code example, the ParentClass is a base class, and the ChildClass is a derived class from the ParentClass. The followed by base class name is the syntax used to derive a class from a base class.

class ParentClass
{
    // Class members for ParentClass can be added here.
}
class ChildClass : ParentClass
{
    // Class members for ChildClass can be added here.
}

Why do we use inheritance?

The answer is reusability. One of the key features of a modern programming language is code reusability. Multiple applications and programs can use a piece of code written by one developer if it provides the same functionality. It not only saves time but also reduces the size of the code. That helps program maintainability and improves reusability.

For example, say you have created a Calculator class that has the functionality of adding, subtracting, dividing, and multiplying. Now, other programs implement an advanced calculator with conversion functionality and advanced calculations with basic functionality; they can derive their classes from the Calculator class.

Types of inheritance

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

Single Inheritance

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

Single Inheritance

Example

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 a single base class.

Multilevel Inheritance in C#

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

Multilevel Inheritance

Example

The following example shows how to derive a class from multiple classes.

using System;
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()
    {
        // Add implementation for the See method if needed.
    }
}

From the above source code, you can see that we have achieved multilevel Inheritance by implementing one derived class into another derived class.

Multiple Inheritance  in C#

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

Hierarchical Inheritance  in C#

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

Hierarchical Inheritance

In short, it means a single base class having more than two or more derived classes.

using System;
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()
    {
        // Add implementation for the See method if needed.
    }
}

Here you can see that above the code, the base class BaseClass and two derived classes, childClass and SecondChildClass, are implemented from the same base class i.e.BaseClass.

As you see, we have two derived classes implemented from the same base class. This type of implementation can also be called 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 parent class's code, it will affect all the child classes.
  2. Many data members remain unused in the class hierarchy, and the memory allocated is not utilized. Hence affects the performance of your program if you have not implemented inheritance correctly.

Summary

In this article, you learned about inheritance and why we use inheritance in C#. 

Next recommended readings


Similar Articles