Inheritance in C# with examples| 5 Types of inheritance in C#

In this article, we will discuss the use of Inheritance in C# through examples.

Types of Inheritance in C#
Types of Inheritance in C#

What is Inheritance in C#?

Inheritance is a feature of object-oriented programming languages that allows derived classes to inherit or override the functionality (data and behavior) of a base class. It is a relationship or association between two classes that allows one class to inherit code from another class.

Inheritance means a parent-child relationship where we can create a new class by using the existing class.

Inheritance is the  IS-A  type of relationship.
(Example: Apple is a fruit, Car is a Vehicle)

ClassDescription
Parent Class: In Inheritance, the class whose features are inherited is known as the parent class(or a generalized class, superclass, or base class).
Child Class: The class that inherits the existing class is known as a child class(or a specialized class, derived class, extended class, or subclass).

Inheritance allows a derived class to extend the functionality of the base class as it can add its own fields, properties, and methods in addition to the base class data members.

Syntax

Inheritance uses  “:”  colon to make a relationship between parent class and child class. Here you can see in the below syntax.

<AccessModifier> class <BaseClassName>
{
    // Base class code
}

<AccessModifier> class <DerivedClassName> :  <BaseClassName>
{
    // Derived class code
}

Example of Inheritance in C#

In the below example, the class “ParentClass” is a base class, the class “ChildClass” is a derived class that extends the “ParentClass” class, and the class “Program” is a driver class to run or execute the program in the Main method.

using System;
namespace Inheritance
{
    // Base class
    class ParentClass
    {
        // Data member
        public string Name;
        // public method of base class 
        public void PrintName() => Console.WriteLine($"Name : {Name}");            
    }
    // Derived class
    class ChildClass : ParentClass
    {
        // public method of derived class 
        public void PrintAge(int Age) => Console.WriteLine($"Age : {Age}");       
    }
    class Program
    {
        static void Main(string[] args)
        {
            ChildClass childClass = new ChildClass();
            // calling the field and method of base class 
            // using the derived class object.
            childClass.Name = "Shekh";          
            childClass.PrintName();
            childClass.PrintAge(29);
        }
    }
}
// Output:
//Name : Shekh
//Age : 29

Advantages of Inheritance

  • Code reusability: Inheritance promotes the concept of code reusability. The derived class inherits the fields, properties, and methods of the base class.
  • Avoiding Duplication of Code: Inheritance support reusability, which reduces duplication of the same code.
  • Polymorphic behavior: Inheritance allows a derived class to override the methods of its base class. This allows developers of derived classes to customize or completely replace the behavior of base class methods.
  • Extensibility: Extensibility is the ability to extend and derive new classes from the existing classes.

Single Inheritance in C#

01.  Single Inheritance : In single inheritance, a derived class inherits the features or behavior of one base class.

public class A
{
// Code Implementation
}
public class B: A
{
// Code Implementation
}

In the following image, class A serves as a base class for the derived class B.

Single Inheritance in c#
Single Inheritance in c#

Multilevel Inheritance In C#

02.  Multilevel Inheritance : In Multilevel Inheritance, one class inherits another class which is further inherited by another class. Inheritance in C# is transitive so the last derived class acquires all the members of all its base classes.

public class A
{
// Code Implementation
}
public class B : A
{
// Code Implementation
}
public class C : B
{
// Code Implementation
}

For example, if class “C” is derived from class “B”, and further class “B” is derived from class “A”, then class “C” will able to inherit the members declared in both class “B” and class “A”.
The following image is an example of multi-level inheritance in C#.

Multilevel Inheritance in C#
Multilevel Inheritance in C#

Multiple Inheritance in C#

03.  Multiple Inheritance :

Multiple inheritance allows a child class to have multiple base classes and inherit features from all of its parent classes.
However, C# does not explicitly support multiple inheritance with classes, and attempting to do so will result in a compilation error. Interfaces are the only way to implement multiple inheritance in C#.

Following is the example of implementing multiple interfaces in C#.

 interface A
    {
       // Code implementation      
    }

 interface B
    {
        // Code implementation      
    }
    // Derived class
 class C : A,B
    {
       // Code Implementation   
    }

Hierarchical Inheritance

04.  Hierarchical Inheritance : In Hierarchical Inheritance, one class serves as a base class for more than one derived class. In the following example, class A refers to a base class for the derived class B, class C, and class D.

 //Base class
    class A
    {
       // Code implementation      
    }
    // Derived class
    class B :A
    {
        // Code implementation      
    }

    // Derived class
    class C : A
    {
       // Code Implementation   
    }
    // Derived class
    class D :A
    {
        // Code Implementation   
    }

Hybrid Inheritance

05.  Hybrid Inheritance : In C#, the hybrid inheritance is not supported with classes, but hybrid inheritance can be achieved through interfaces only. Actually, hybrid inheritance is a mix of multiple types of inheritance.

following is an example of hybrid inheritance in C#.

interface A
    {
       // Code here      
    }
   
    interface B :A
    {
        // Code here      
    }
  
    interface C : A
    {
       // Code here   
    }

    // Derived class
    class D :B, C
    {
        // Code here   
    }

Key points about Inheritance in C#

  • In C #, the structure does not support inheritance, but it can be inherited through interfaces.
  • The Object class is the base class of all subclasses. All classes in C# are implicitly derived from the Object class.
  • C# only supports single inheritance, but we can implement multiple inheritance through interfaces.
  • A child class can’t inherit the private members of its parent class until or unless the base class doesn’t have public properties to access the private fields.
  • A child class can inherit all the members of its base class, except the constructor, because the constructor is not a data member of the class. (Note: A constructor of the base class can be called from the child class.)

Conclusion

In this post, we learned about the different types of Inheritance in C#, such as Single InheritanceMultiple Inheritance, Multilevel Inheritance, Hierarchical Inheritance, and Hybrid Inheritance with multiple examples.

References MSDN: Inheritance in C# and .NET

Recommended Articles:

Shekh Ali
3 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments