Please find the code below:
using System;
namespace MainNS
{
abstract class BaseClass
{
abstract public void fun();
}
class DerivedClass1 : BaseClass
{
override public void fun()
{
Console.WriteLine("fun() of DerivedClass1 invoked!");
}
}
class DerivedClass2 : DerivedClass1
{
new public void fun()
{
Console.WriteLine("fun() of DerivedClass2 invoked!");
}
}
class MainClass
{
public static void Main(string[] args)
{
DerivedClass1 d1 = new DerivedClass2();
d1.fun();
Console.ReadKey();
}
}
}
What is the use of replacing new
with override
here and Please explain the actual concept behind this.
Override
keyword makes fun()
of DerivedClass2
to be executed and new
keyword makes fun()
of DerivedClass1
to be executed.
class DerivedClass2 : DerivedClass1
{
new public void fun()
{
Console.WriteLine("fun() of DerivedClass2 invoked!");
}
}