Multilevel Inheritance in C# - With Sample Program

Understanding Multilevel Inheritance in C#

Understanding Multilevel Inheritance in C#

Definition of Multilevel Inheritance

Multilevel inheritance occurs when a class is derived from another derived class. In this structure, there are multiple levels of inheritance, forming a hierarchy. For example, if Class A is the base class, Class B is derived from Class A, and Class C is derived from Class B, this is termed as multilevel inheritance.

Types of Inheritance in C#

C# supports five types of inheritance:

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

Structure of Multilevel Inheritance

In a multilevel inheritance structure:

Class A (Base Class) → Class B (Derived Class from A) → Class C (Derived Class from B)

Here, Class B is a child of Class A and a parent of Class C. This establishes a chain of inheritance.

Example of Multilevel Inheritance

Consider a practical example where:

  • Grandfather is the base class.
  • Father is derived from Grandfather.
  • Son is derived from Father.
class Grandfather {
    public void Display1() {
        Console.WriteLine("Grandfather");
    }
}

class Father : Grandfather {
    public void Display2() {
        Console.WriteLine("Father");
    }
}

class Son : Father {
    public void Display3() {
        Console.WriteLine("Son");
    }
}

Calling Methods in Multilevel Inheritance

To call the methods from the derived classes, an object of the last derived class (Son) is created, and methods from all levels can be invoked:

Son s = new Son();
s.Display1(); // Outputs: Grandfather
s.Display2(); // Outputs: Father
s.Display3(); // Outputs: Son

Here's an another example:

public class Animal
{
public void Eat()
{
Console.WriteLine("Animals eat food.");
}
}
// Derived class inheriting from Animal
public class Mammal : Animal
{
public void Walk()
{
Console.WriteLine("Mammals walk on legs.");
}
}
// Another derived class inheriting from Mammal
public class Dog : Mammal
{
public void Bark()
{
Console.WriteLine("Dogs bark.");
}
}
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog();
// Accessing methods from all levels of inheritance
dog.Eat(); // Method from Animal class
dog.Walk(); // Method from Mammal class
dog.Bark(); // Method from Dog class
Console.ReadLine();
}
}
Animals eat food.

Mammals walk on legs.
Dogs bark.

Summary of Key Points

Multilevel inheritance allows for a clear hierarchical structure. Each derived class can inherit properties and methods from its parent class, promoting code reusability and organization. It is essential to understand the flow of inheritance to effectively utilize it in C# programming.

  
  	This Content Sponsored by Buymote Shopping app

	BuyMote E-Shopping Application is One of the Online Shopping App

	Now Available on Play Store & App Store (Buymote E-Shopping)

	Click Below Link and Install Application: https://buymote.shop/links/0f5993744a9213079a6b53e8

	Sponsor Content: #buymote #buymoteeshopping #buymoteonline #buymoteshopping #buymoteapplication
    
  

Post a Comment

0 Comments