Write A C# Program To Implement Multilevel Inheritance

Do you want to know how to implement multilevel inheritance in C#? In this C# tutorial, I will explain how to write a C# program to implement multilevel inheritance.

To demonstrate multilevel inheritance in C#, consider a base class ElectronicDevice with basic functionalities, an intermediate class Computer inheriting from it and adding computer-specific features, and a derived class Laptop that extends Computer with laptop-specific attributes like battery level. Each class represents a level in the inheritance hierarchy, demonstrating the concept of multilevel inheritance.

What is Multilevel Inheritance in C#?

In C#, multilevel inheritance allows a class (derived class) to inherit from another class (base class), which itself is a derived class of another. This hierarchy can continue to multiple levels, enabling the creation of complex class structures. Each level of the hierarchy inherits the properties and methods of its parent class, allowing for code reusability and logical grouping of functionalities.

Why Use C# Multilevel Inheritance?

C# multilevel inheritance offers several benefits:

  1. Reusability: It allows programmers to reuse the code from the base classes, reducing redundancy.
  2. Extensibility: You can extend the functionality of existing classes without modifying them.
  3. Hierarchical Structure: It helps in creating a natural hierarchical classification.

Example of Multilevel Inheritance in C#

Consider a school system where we have a basic Person class, a Teacher class that inherits from Person, and a ScienceTeacher class that inherits from Teacher.

Step 1: Creating the Base Class – Person

The Person class serves as our base class. It includes basic properties that are common to all people in the school system, such as name and age.

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }

    public void DisplayInfo()
    {
        Console.WriteLine($"Name: {Name}, Age: {Age}");
    }
}

Step 2: First Level of Inheritance – Teacher

The Teacher class is the first level of derived class. It inherits from Person and adds additional properties and methods specific to teachers, like the subject they teach.

public class Teacher : Person
{
    public string Subject { get; set; }

    public Teacher(string name, int age, string subject) : base(name, age)
    {
        Subject = subject;
    }

    public void DisplayTeacherInfo()
    {
        DisplayInfo();
        Console.WriteLine($"Subject: {Subject}");
    }
}

Step 3: Second Level of Inheritance – ScienceTeacher

The ScienceTeacher class represents the second level of inheritance, deriving from Teacher. This class can have properties and methods specific to science teachers.

public class ScienceTeacher : Teacher
{
    public string Laboratory { get; set; }

    public ScienceTeacher(string name, int age, string subject, string laboratory) 
        : base(name, age, subject)
    {
        Laboratory = laboratory;
    }

    public void DisplayScienceTeacherInfo()
    {
        DisplayTeacherInfo();
        Console.WriteLine($"Laboratory: {Laboratory}");
    }
}

Step 4: Implementing the Classes

Now, we can create an instance of ScienceTeacher and see multilevel inheritance in action.

class Program
{
    static void Main(string[] args)
    {
        ScienceTeacher mrSmith = new ScienceTeacher("John Smith", 40, "Physics", "Lab A");
        mrSmith.DisplayScienceTeacherInfo();
    }
}

Once you run the code, this code will output:

Name: John Smith, Age: 40
Subject: Physics
Laboratory: Lab A

Conclusion

Multilevel inheritance in C# enables developers to create a structured and maintainable hierarchy of classes. In our school system example, we demonstrated how a ScienceTeacher class inherits properties and methods from both Teacher and Person classes.

I hope now you know how to implement multilevel inheritance in C# with this real example.

You may also like: