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. Inheritance 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 code.

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

Parent Class In Inheritance, the class whose features are inherited is known as parent class(or a generalized class, superclass, or base class).
Child Class The class that inherits the existing class is known as 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 of Inheritance

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

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

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

Example of Inheritance in C#

In the below example of inheritance, class “ParentClass” is a base class, class “ChildClass” is a derived class that extends the “ParentClass” class, and 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

What are the benefits of using Inheritance?

Code reusability: Encapsulate common code in one class and can use it in the other class.
Inheritance supports the concept of code reusability.

Suppose that we want to create a new class and there is already a class that contains some of the methods, properties, or fields that we want, we can simply derive our new class from the existing class to reuse the members of that class.

Polymorphic behaviour: Inheritance allows a derived class to override a method of its base class. That enables the developer of the derived class to customize or completely replace the behavior of the base class method.
It also creates a form of dynamic polymorphism. Both methods implemented by the base and derived class, share the same name and parameters but can provide different functionality.

Types of Inheritance in C#

    • Single Inheritance
    • Multilevel Inheritance
    • Multiple Inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance

Single Inheritance in C#

1.  Single Inheritance  : In single inheritance, a derived class inherit 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#

2.  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 inherits 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#

3.  Multiple Inheritance :

With multiple inheritance, a derived class can have more than one base class and access their functionality.
Multiple inheritance with classes is not explicitly supported in C #. The only way to implement multiple inheritance is through interfaces. If we try to inherit a derived class from many base classes, we will get a compilation error.

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

4.  Hierarchical Inheritance : In Hierarchical Inheritance, one class serves as a base class for more than one derived classes. In the following example, class A refers as 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

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

following are the example of the hybrid inheritance.

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

  
    interface C : A
    {
       // Code here   
    }

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

Important 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 the its parent class until or unless the base class doesn’t have public properties to access the private fields.
    • A subclass can inherit all the members of its base class, except the constructor, because the constructor is not a data member of the class. But the constructor of the base class can be called from the subclass.

Conclusion

Today we learned all about different types of Inheritance in C#, such as Single InheritanceMultiple Inheritance, Multilevel Inheritance, Hierarchical Inheritance, and Hybrid Inheritance with multiple examples.

If you have any questions, please post your comments below.
I hope you enjoyed this post and found it useful. 🙂

References MSDN: Inheritance in C# and .NET

Recommended Articles

  1. Generic Delegates in C# With Examples
  2. C# List Class |Top 12 Amazing Generic List Methods
  3. IEnumerable Interface in C# with examples
  4. Constructors in C# with Examples
  5. Properties In C# with examples
  6. 10 Difference between interface and abstract class In C#
  7. Value Type and Reference Type in C#
  8. Multithreading in C#
  9. What is a delegate in C#? Multicast Delegate with Examples

Leave a Reply