4
Reply

What is mean by virtual override ??

sathish

sathish

May 23, 2010
6.7k
0

    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

    laxmikant Mishra
    June 06, 2010
    0

    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();
            }
        }

    Shovit Kumar
    May 28, 2010
    0

    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?

    Dushyant Sikligar
    May 27, 2010
    0