4
Reply

What is mean by virtual override ??

sathish

sathish

14y
6.7k
0
Reply

    Hi virtual And override both are keyword and virtual is use to declare virtual method in base class and when we implement the virtual method in the derived method  we have to use the override modifier in method declaration

    I agree with Dushyant Sikligar that there is nothing called virtual override in C# as we need to explicitly define a "virtual" keyword in any of the methods we declare. If we want to override the MyMethod() in MyDerivedClass then we need to define it "virtual", otherwise it will throw complile time error.

    In C#.net, overriding is the basic feature of Inheritance, by default any method is non virtual. That means if any method is not declared as virtual then overriding will not possible (of course shadowing will be possible, either explicity shadowing or silent shadowing).

    If One method is declared as virtual, then the overriden method of that virtual method will also by default virtual. This type of overriding is called virtual override.
    for example

         public class MyBaseClass
        {
            public virtual void MyMethod()
            {
            }
        }

        public class MyDerivedClass : MyBaseClass
        {
            public override void MyMethod()
            {
                base.MyMethod();
            }
        }
        public class MySecondDerivedClass : MyDerivedClass
        {
            public override void MyMethod()               //Howerver this method is not declared as virtual in MyDerivedClass
            {
                base.MyMethod();
            }
        }

    In java, the default for methods is override. In c# that is not the case.
    This talks about two classes, the base class and the overriding class. What
    happens when you have a third class in the mix, extending the second class,
    how can you indicate the method in the third class is overridding the
    overridden method? Do you declare the second method to be virtual override?

    ClassA
    -------------
    virtual DrawWindow(){//code}
    ^
    |
    |
    ClassB
    ---------
    override DrawWindow(){//code}

    What happens if you want to creat a ClassC which extends ClassB, and you
    want to override DrawWindow()? How do you declare the ClassB.DrawWindow
    method?