开发者

C# virtual keyword

Is a virtual method compulsory to ov开发者_JAVA技巧erride by its subclass?


If a method is not virtual (or abstract), it can't be overriden in the sub class. The only option then is to use the new keyword to hide the inherited method, but this is generally not a recommended practice, since casting the instance of the sub class to the parent class and calling the method will cause the parent class method to be called instead, and that is probably not the intended functionality.

So in short, yes it needs to be virtual to override, but no you dont have to override it. If you don't, the base class version will be called instead. But if you do override it, even if you cast the object to it base class type, the derived version of the method will be called, unlike for hiding the method with new.

It's good to know the difference between overriding and hiding the method, since at first glance, and if you test it, it might look like it does the same thing, but will come back to bite you later when the program don't work as expected, and you have no idea why.


No, when you are using "virtual" keyword, it's up to you whether you want to override or not. When you use "abstract", you need to override the method in the derived class. For more information, please see:

  • virtual (C#) (the keyword)

  • abstract (C#) (the modifier)


No. It can be overriden by a sub class, but does not have to be.

If you want to enforce overriding by subclasses, use abstract in an abstract class, or use an interface (meaning you have to implement all declared members).


Is a virtual method is compulsory to override by it's sub class?

No.

Can I Override a method which is NON-Virtual in parent class ?

No.

What is YES then ?

You must implement Abstract method of parent class (if derived class is non-abstract)

Can I write Virtual keyword on Static method

NO, Because these two are just opposite words. Virtual means compiler does not know at compile time which method to be called. Static means , for sure , your compiler knows which method will be called.

How do I stop my current class' subclasses to no Override my method ?

Mark it with sealed keyword.

Is Abstract method a virtual also ?

YES. That's why we cant explicitly mark abstract method virtual as well.

Is Override and Overload same ?

Off-course not! Overloading is having 2 methods with same name but which works on different set of input parameters.

When shall I Mark a method as Virtual ?

When you are using polymorphism and you are not sure about the type of the object passes to your method until runtime, and you want your subclasses to have different behavior then mark the method as Virtual. e.g. You have

class Law
{
 public void Punish(Minister any)
 {
    if (any.Corruption() == true)
      {
        ... do whatever public wants...
      }
 }
}

And you have Hierarchy of Minister classes -Ministers, CentralGOVTMinisters , StateGOVTMinisters , DistrictAuthorityMinisters etc. And you know that Whatever defined in Minister class for Corruption() method can be used by many derived class but for few ministers Corruption laws are different (they have supreme powers may be !) , so there you override the Corruption() and everywhere else your derived classes use the implementation of Corruption() in Minister class (base of all minister).


Yes, if a method is not marked virtual or abstract it cannot be overridden. The only exception is when declaring an interface method you don't need to use it because an interface method is virtual by definition. When you implement this interface you need to use virtual or abstract if you want to be able to derive from the class implementing the interface and be able to override the method.


It is not compulsory to override virtual methods that are not abstract.

It is compulsory to implement abstract methods unless you also mark your class abstract.


No, it is not mandatory to override a virtual method in a derived class.


yes to override you need to write virtual keyword. or write keyword abstract method.


No, it just means that you can override it, not that you have to. Abstract is the keyword if you want to make it mandatory for the subclass to implement it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜