-2

In C #, why we don't have a compilation error even if we forget the keyword "Virtual" in a method of a base class, redefined in a derived class. I learnded that this keyword is mandatory to override a method
is not it ?

public class Mother
{
    public  void  Speak()
    {
        Console.WriteLine("Mother !");
    }
}
public class Son : Mother
{
    public  void Speak()
    {
        Console.WriteLine("Son!!");
    }
}

Should be :

public class Mother
{
    public virtual  void  Speak()
    {
        Console.WriteLine("Mother !");
    }
}
public class Son : Mother
{
    public  override void Speak()
    {
        Console.WriteLine("Son!!");
    }
}

1 Answers1

0

Basically, Virtual and Override works on dynamic binding. The dynamic binding will be decided at run-time Compiler doesn't know that which virtual method will override in the derived class. That why it doesn't give you an error. This will be decided at run-time. Which derived class will be using at run-time.

Danish Ahmed
  • 490
  • 5
  • 6