Why would you declare a method as \"virtual\".
What is the benefit in using virtual?
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();
}
}