C# Program to Demonstrate Multilevel Inheritance with Virtual Methods

This is a C# Program to illustrate multilevel inheritance with virtual methods.

Problem Description

This C# Program Illustrates Multilevel Inheritance with Virtual Methods.

Problem Solution

Here the system executes the first override-virtual method found in the hierarchy.

Program/Source Code

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

/*
 * C# Program to Illustrate Multilevel Inheritance with Virtual Methods
 */
using System;
public class Person
{
    protected string RNO = "44";
    protected string name = "Ram";
    public virtual void GetInfo()
    {
        Console.WriteLine("Name: {0}", name);
        Console.WriteLine("RNO: {0}", RNO);
        Console.WriteLine();
    }
}
class Student : Person
{
    public string id = "ABC";
    public override void GetInfo()
    {
        base.GetInfo();
        Console.WriteLine("Student ID: {0}", id);
    }
}
class Stud : Student
{
    private string StudentAddress = "USA";
    public void GetInfo()
    {
        base.GetInfo();
        Console.WriteLine("Student Address: {0}", StudentAddress);
    }
}
class TestClass
{
    public static void Main()
    {
        Student E = new Student();
        E.GetInfo();
        Stud Stud = new Stud();
        Stud.GetInfo();
        Console.ReadLine();
    }
}
Program Explanation

This C# program is used to illustrate multilevel inheritance with virtual methods. The system executes the first override-virtual method found in the hierarchy. Create an object variable ‘e’ for the student() procedure.

advertisement
advertisement

Using object variable perform the operation of the GetInfo() procedure. The GetInfo() procedure is used to retrieve information about the data container. Here the system executes the first override-virtual method found in the hierarchy. Print the multilevel inheritance with virtual methods.

Runtime Test Cases
 
Name : Ram
RNO : 44
 
Student ID : ABC
Name : Ram
RNO : 44
 
Student ID : ABC
Student Address : USA

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.