Virtual Methods

Declaring a class member as *virtual* allows inheriting classes to override the base class behavior.
This is one of the pillars of OOP known as polymorphism.

Declaring a method as *sealed* breaks this inheritance chain., preventing inheriting classes from overriding base class methods.


class BaseClass
{
public virtual void Method_01()
{
Console.WriteLine("BaseClass.Method_01");
}
public virtual void Method_02()
{
Console.WriteLine("BaseClass.Method_02");
}
}
class DerivedClass : BaseClass
{
public override void Method_01()
{
Console.WriteLine("DerivedClass.Method_01");
}
public override sealed void Method_02()
{
Console.WriteLine("DerivedClass.Method_02");
}
}
class Consumer : DerivedClass
{
public override void Method_01()
{
Console.WriteLine("Consumer.Method_01");
}
public new void Method_02()
{
Console.WriteLine("Consumer.Method_02");
}
}



static void Main(string[] args)
{
BaseClass bc;
bc = new DerivedClass();
bc.Method_01();
bc.Method_02();
Console.ReadLine();
bc = new Consumer();
bc.Method_01();
bc.Method_02();
Console.ReadLine();
}

: