C# Program to Illustrate Multilevel Inheritance with Virtual Methods

In this tutorial, we will write a C# program to illustrate Multilevel inheritance with Virtual Function. You may go through the following topic in C#.

Virtual functions are created in order to override the functions with the same name just like in the given below program.


C# Program for Multilevel Inheritance with Virtual Function

using System;

class A
{
  public virtual void Function()
  {
    Console.WriteLine("Class A.Funtion()");
  }
}

class B: A
{
  override public void Function()
  {
    base.Function();
    Console.WriteLine("Class B:A.Function()");
  }

  public virtual void _Function()
  {
    Console.WriteLine("Class B._Function()");
  }
}

class C: B
{
  new public void _Function()
  {
    base._Function();
  }
}

class Program
{
  static void Main(String[] s)
  {
    C c = new C();
    c.Function();
  }
}

Output:

Class A.Funtion()
Class B:A.Function()