Inheritance in C# with examples.

In this article, we are going to talk about the use of Inheritance in C# with examples.

What is Inheritance?

Inheritance is a kind of relationship or association between two classes that allows one class to inherit code from the other 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 below example of inheritance, class “ParentClass” is a base class, class “ChildClass” is a derived class which extends “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

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, the class A serves as a base class for the derived class B.

Single Inheritance in c#
Single Inheritance in c#

Multilevel Inheritance

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

3.  Multiple Inheritance :In Multiple inheritance, one derived class can have more than one base classes and can access there functionality. But C# does not directly support multiple inheritance with classes. In C#, we can implement multiple inheritance only through Interfaces. If we try to inherit one derived class with multiple base classes, it will throw compile time error.

Following is the example of implementing multiple interface 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#, struct doesn’t support inheritance but it can be inherited through interfaces.
    • Object class is the base class of all the subclasses but it does have any base or super class of it’s own. All the classes in C# are implicitly derived from the Object class.
    • C# support single inheritance only but we can achieve multiple inheritance via interfaces.
    • A child class can’t inherit the private members of the its parent class until or unless 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 constructors as constructors are not the data members of a class. but constructor of a base class can be invoke from the child class.

Conclusion

Today we learned all about the Inheritance, such as Single Inheritance, Multiple Inheritance, Multilevel Inheritance, Hierarchical Inheritance and Hybrid Inheritance with multiple examples.
I hope you enjoyed this post and found it useful.

Thanks for visiting.

Leave a Reply