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.

advertisement
advertisement

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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.