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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 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()