I have learnt that the usage of virtual methods provide a default behaviour for a method/function. My query is that, when we can implement a method of same name (as in base class) using "new" in derived class and can call it by objects separately then whats the use of virtual method. For instance;
class A
{
public void F()
{
//default behavior
}
public virtual void G()
{
//default behavior
}
}
class B:A
{
new public void F()
{
//new behavior from that of class A
}
public override void G()
{
//new behavior from that of class A
}
}
class Test
{
public static void Main(string[] args)
{
//For calling A's method use same compile time and run time object
A obj1 = new A();
//For calling B's method use compile time object of A and run time of B
A obj2 = new B();
obj1.F(); //O/P is A's method result
obj2.F(); //O/P is A's method result
obj1.G(); //O/P is A's method result
obj2.G(); //O/P is B's method result
}
My question is why virtual G() in class A is needed when we can provide default behavior as well as derived behavior by using same compile time object and run time object of both classes A and B as like:
A obj1 = new A(); //for calling A's methods
B obj2 = new B(); //for calling B's methods
A obj3 = new B(); //for having default behavior of class A using B's run time object.
What I fetched is only that in case of virtual we just don't need to make one more object of same compile and run time type. It can be achieved by a single object which is compile time of type A and run time of type B.
Virtual methods allow subclasses of the type to override the method. They are used to implement run time polymorphism or late binding. It should be noted that virtual or abstract members of a class cannot be declared as private.
Yes, you can have virtual interface members in C# 8. Any interface member whose declaration includes a body is a virtual member unless the sealed or private modifier is used. So by default, all the default interface methods are virtual unless the sealed or private modifier is used.
When the method is declared as virtual in a base class, and the same definition exists in a derived class, there is no need for override, but a different definition will only work if the method is overridden in the derived class. Two important rules: By default, methods are non-virtual, and they cannot be overridden.
You can't declare private virtual methods because there's no point (since there'd be no way to override them)... But you can override protected virtual methods.
My question is why virtual G() in class A is needed when we can provide default behavior by using same compile time and run time object and derived behavior by using compile time object of A and run time of B.
Because then you can only use code which already knows about B
. Half the point of polymorphism is allow you to write code which doesn't care about which subclass it actually works with.
For example, I can write a method to copy the contents of one stream to another using Stream.Read
and Stream.Write
precisely because they're virtual or abstract methods. It doesn't matter which implementations are passed in.
You should very rarely use new
as a method modifier - if you really want to provide different behaviour for a non-virtual method, it's much clearer if you create a totally different method name, so that anyone reading the code understands this is a very separate method from the one declared in the base class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With