Why would you declare a method as \"virtual\".
What is the benefit in using virtual?
In order to be able to override it in inheriting classes.
Check out the MSDN entry for the keyword. That explains it more in depth.
A virtual method is a type of method where the actual method calls depends on the runtime type of the underlying object.
A non-virtual method is a type of method where the actual method called depends on the reference type of the object at the point of method invocation.
Virtual allows an inheriting class to replace a method that the base class then uses.
public class Thingy
{
public virtual void StepA()
{
Console.Out.WriteLine("Zing");
}
public void Action()
{
StepA();
Console.Out.WriteLine("A Thingy in Action.");
}
}
public class Widget : Thingy
{
public override void StepA()
{
Console.Out.WriteLine("Wiggy");
}
}
class Program
{
static void Main(string[] args)
{
Thingy thingy = new Thingy();
Widget widget = new Widget();
thingy.Action();
widget.Action();
Console.Out.WriteLine("Press any key to quit.");
Console.ReadKey();
}
}
When you run the Program your output will be:
Zing
A Thingy in Action.
Wiggy
A Thingy in Action.
Notice how even though Widget called the Action() method defined at the Thingy level, internally Thingy called Widget's StepA() method.
The basic answer is it gives inheritors of a class more flexibility. Of course, you've got to engineer your class well or it could weak havoc.
Virtual Methods on MSDN
The virtual keyword is used to modify a method or property declaration, in which case the method or the property is called a virtual member. The implementation of a virtual member can be changed by an overriding member in a derived class.
When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member. (For more information on run-time type and most derived implementation, see 10.5.3 Virtual methods.)
By default, methods are non-virtual. You cannot override a non-virtual method.
You cannot use the virtual modifier with the following modifiers:
static abstract override
Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax.
- It is an error to use the virtual modifier on a static property.
- A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
Virtual functions are the functions that doesn't really exists.The derived class can modify the virtual function by overriding it.Virtual functions are one of the way to achieve run time polymorphism
public class sample {
public virtual void fun(){
Console.WriteLine("base sample class \n");
}
}
public class A : sample{
public override void fun(){
Console.WriteLine("Class A \n");
}
}
public class B : sample{
public override void fun(){
Console.WriteLine("Class B \n");
}
}
class run{
public static void main(String[] args){
sample obj = new sample();
sample obj1 = new A();
sample obj2 = new B();
obj.fun();
obj1.fun();
obj2.fun();
}
}
The Virtual Modifier is used to mark that a method\property(ect) can be modified in a derived class by using the override modifier.
Example:
class A
{
public virtual void Foo()
//DoStuff For A
}
class B : A
{
public override void Foo()
//DoStuff For B
//now call the base to do the stuff for A and B
//if required
base.Foo()
}