While going through the differences between Abstract and virtual methods at Difference between Abstract and Virtual Function.
I got a doubt related to virtual
and new
Let's consider a sample code as below
class MainClass
{
public static void Main()
{
DerivedClass _derived = new DerivedClass();
_derived.SayHello();
_derived.SayGoodbye();
Console.ReadLine();
}
}
public abstract class AbstractClass
{
public void SayHello()
{
Console.WriteLine("Hello - abstract member\n");
}
public virtual void SayGoodbye()
{
Console.WriteLine("Goodbye- abstract member \n");
}
//public abstract void SayGoodbye();
}
public class DerivedClass : AbstractClass
{
public new void SayHello()
{
Console.WriteLine("Hi There - Hiding base class member");
}
//public override void SayGoodbye()
//{
// Console.WriteLine("See you later - In derived class OVERRIDE function");
//}
public new void SayGoodbye()
{
Console.WriteLine("See you later - In derived class I'm in NEW member");
}
}
My Question:
In the derived class, How override
and new
perform the same functionality if i call SayGoodbye
function? When i need to go to choose/ prefer among them? In which real time scenarios i need to prefer among them?