Tuesday, January 25, 2022

Multi-Level Inheritance in C#

 It is 2nd Part of  Inheritance in C# for Multi-Level

Generally, c# supports only single inheritance that means a class can only inherit from one base class. However, in c# the inheritance is transitive, and it allows you to define a hierarchical inheritance for a set of types, and it is called a multi-level inheritance.

 

For example, if class C is derived from class B, and class B is derived from class A, then class C inherits the members declared in both class B and class A.


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


If you observe the above code snippet, class C is derived from class B, and class B is derived from class A, then class C inherits the members declared in both class B and class A. This is how we can implement multi-level inheritance in our applications.


Multi-Level Inheritance Example

Following is the example of implementing multi-level inheritance in the c# programming language.

using System;

namespace ArjunInheritance
{
    public class A
    {
       public string Name;
       public void GetName()
       {
          Console.WriteLine("Name: {0}", Name);
       }
    }
    public class B: A
    {
       public string Location;
       public void GetLocation()
       {
          Console.WriteLine("Location: {0}", Location);
       }
    }
    public class C: B
    {
       public int Age;
       public void GetAge()
       {
          Console.WriteLine("Age: {0}", Age);
       }
    }
    class Program
    {
       static void Main(string[] args)
       {
          C c = new C();
          c.Name = "Arjun Walmiki";
          c.Location = "Thane";
          c.Age = 35;
          c.GetName();
          c.GetLocation();
          c.GetAge();
          Console.WriteLine("\nPress Any Key to Exit..");
          Console.ReadLine();
       }
    }
}

If you observe the above example, we implemented three classes (ABC), and class C is derived from class B, and class B is derived from class A.

 

By implementing a multi-level inheritance, class C can inherit the members declared in class B and class A.

 

When you execute the above c# program, you will get the result as shown below.


Name : Arjun Walmiki

Location : Thane

Age : 35

Press Any Key to Exit..


Multiple Inheritance

As discussed, c# supports only single inheritance that means a class can only inherit from one base class. If we try to inherit a class from multiple base classes, then we will get compile-time errors.

 

For example, if class C is trying to inherit from Class A and B simultaneously, we will get a compile-time error because multiple inheritance is not allowed in c#.

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

}

If you observe the above code snippet, class C is trying to inherit properties from both classes A and B simultaneously, which will lead to compile-time errors like “Class C cannot have multiple classes: A and B”.

 

As discussed, multi-level inheritance is supported in c#, but multiple inheritance is not supported. If you want to implement multiple inheritance in c#, we can achieve this by using interfaces. In the next chapters, we will learn how to use interfaces to achieve multiple inheritance in a detailed manner. 

No comments:

Post a Comment