C# Program to Demonstrate Hierarchical Inheritance
Posted by Superadmin on August 13 2022 11:07:59

C# Program to Demonstrate Hierarchical Inheritance

 

 

 

This is a C# Program to illustrate hierarchical inheritance.

Problem Description

This C# Program Illustrates Hierarchical Inheritance.

Problem Solution

Here Single parent and multiple child and when more than one derived class are created from single base class is called C# Hierarchical Inheritance.

Program/Source Code

Here is source code of the C# Program to Illustrate Hierarchical Inheritance. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Illustrate Hierarchical Inheritance
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritance
{
    class Program
    {
        static void Main(string[] args)
        {
            Principal g = new Principal();
            g.Monitor();
            Teacher d = new Teacher();
            d.Monitor();
            d.Teach();
            Student s = new Student();
            s.Monitor();
            s.Learn();
            Console.ReadKey();
        }
        class Principal
        {
            public void Monitor()
            {
                Console.WriteLine("Monitor");
            }
        }
        class Teacher : Principal
        {
            public void Teach()
            {
                Console.WriteLine("Teach");
            }
        }
        class Student : Principal
        {
            public void Learn()
            {
                Console.WriteLine("Learn");
            }
        }
    }
}
Program Explanation

This C# program is used to illustrate hierarchical inheritance. We have created two classes Principal and Teacher. Using the object variable ‘g’ of Principal class perform the Monitor() procedure. Using the object variable ‘d’ of Teacher class perform the Monitor() and Teach() procedure.

 

Finally using ‘s’ object variable perform the Monitor() and Learn() procedure. Here Single parent and multiple child and when more than one derived class are created from single base class are called C# Hierarchical Inheritance.

Runtime Test Cases
 
Monitor
Monitor
Teach
Monitor
Learn