Virtual methods allow object oriented languages to express polymorphism.This means that a derived class can write a method with the same signature as a method in its base class, and the base class will call the derived class's method.By default in java, all methods are virtual. In C# like C++, the virtual keyword is needed to specify that a method should override a method (or implementaion an abstract method) of its base class.
Class B { public virtual void foo () {}
} ClassD : B { public override void foo () {} } |
Attemting to override a non-virtual method will results in a compile-time error unless the "new" keyword is added to the declaration, indicating the method is intentionally hiding the base clas's method.