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()
}
The runtime takes place over compile time.
When you declare a method as virtual, declaring it in derived class require you to add a override or new modifier.
we can see that when TrySpeak. Passing in child and father, both call Speak of father, while TryScream, would call each method.
To understand this, there are some things we should know, in an instance of Child, There are two Scream methods from Child class or Father class. We could either call the Scream from Child class or Father class.
Because Virtaul Modifier mark the method so it can be overriding by the derived class, which means even the Scream is called from Father class, it is overriden, it would be defferent if you use new modifier.
import system;
class Father
{
Speak()
{
Console.Writeline("Father is speaking")
}
virtual Scream()
{
Console.Writeline("Father is screaming")
}
}
class Child: father
{
Speak()
{
Console.Writeline("Child is speaking")
}
override Scream()
{
Console.Writeline("Child is screaming")
}
}
class APP
{
public static void Main()
{
// We new two instances here
Father father = new Father();
Child child = new Child();
// Here we call their scream or speak through TryScream or TrySpeak
TrySpeak(father);
TrySpeak(child);
//>>>"Father is speaking"
//>>>"Father is speaking"
TryScream(father);
TryScream(child);
//>>>"Father is screaming"
//>>>"Child is screaming"
}
// when your method take an Parameter who type is Father
// You can either pass in a Father instance or
// A instance of a derived Class from Father
// which could be Child
public static void TrySpeak(Father person)
{
person.Scream();
}
public static void TryScream(Father person)
{
person.Speak();
}
}
the difference between virtual and non-virtual methods.
We have two classes; one is a Vehicle class and another is a Cart class. The "Vehicle" class is the base class that has two methods; one is a virtual method "Speed()" and another is a non-virtual method "Average()". So the base class virtual method "Speed()" is overriden in the sub class. We have one more class "Program" (the execution class) that has an entry point where we create an instance of sub class "Cart" and that instance is assigned to the base class "Vehicle" type. When we call virtual and non-virtual methods by both class's instance then according to the run type the instance virtual method implementation is invoked; in other words both class's instances invoke the subclass override method and the non-virtual method invoked is determined based on the instance of the class.
using System;
namespace VirtualExample
{
class Vehicle
{
public double distance=0.0;
public double hour =0.0;
public double fuel =0.0;
public Vehicle(double distance, double hour, double fuel)
{
this.distance = distance;
this.hour = hour;
this.fuel = fuel;
}
public void Average()
{
double average = 0.0;
average = distance / fuel;
Console.WriteLine("Vehicle Average is {0:0.00}", average);
}
public virtual void Speed()
{
double speed = 0.0;
speed = distance / hour;
Console.WriteLine("Vehicle Speed is {0:0.00}", speed);
}
}
class Car : Vehicle
{
public Car(double distance, double hour, double fuel)
: base(distance, hour, fuel)
{
}
public void Average()
{
double average = 0.0;
average = distance / fuel;
Console.WriteLine("Car Average is {0:0.00}", average);
}
public override void Speed()
{
double speed = 0.0;
speed = distance / hour;
Console.WriteLine("Car Speed is {0:0.00}", speed);
}
}
class Program
{
static void Main(string[] args)
{
double distance,hour,fuel=0.0;
Console.WriteLine("Enter the Distance");
distance = Double.Parse(Console.ReadLine());
Console.WriteLine("Enter the Hours");
hour = Double.Parse(Console.ReadLine());
Console.WriteLine("Enter the Fuel");
fuel = Double.Parse(Console.ReadLine());
Car objCar = new Car(distance,hour,fuel);
Vehicle objVeh = objCar;
objCar.Average();
objVeh.Average();
objCar.Speed();
objVeh.Speed();
Console.Read();
}
}
}
Needless to say, virtual methods come in handy when your code is trying to abide with the Open Closed Principle
Read More about the Open Closed Principle here, Uncle Bob's original OCP whitepaper.
Also pls be aware that methods are not virtual by default in C# unlike Java.
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.
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:
The runtime takes place over compile time.
When you declare a method as virtual, declaring it in derived class require you to add a
override
ornew
modifier.we can see that when
TrySpeak
. Passing in child and father, both call Speak of father, whileTryScream
, would call each method.To understand this, there are some things we should know, in an instance of Child, There are two
Scream
methods from Child class or Father class. We could either call theScream
from Child class or Father class. BecauseVirtaul
Modifier mark the method so it can be overriding by the derived class, which means even theScream
is called from Father class, it is overriden, it would be defferent if you use new modifier.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.
the difference between virtual and non-virtual methods.
We have two classes; one is a Vehicle class and another is a Cart class. The "Vehicle" class is the base class that has two methods; one is a virtual method "Speed()" and another is a non-virtual method "Average()". So the base class virtual method "Speed()" is overriden in the sub class. We have one more class "Program" (the execution class) that has an entry point where we create an instance of sub class "Cart" and that instance is assigned to the base class "Vehicle" type. When we call virtual and non-virtual methods by both class's instance then according to the run type the instance virtual method implementation is invoked; in other words both class's instances invoke the subclass override method and the non-virtual method invoked is determined based on the instance of the class.
hope to help!