Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# virtual keyword

Tags:

c#

Is a virtual method compulsory to override by its subclass?

like image 220
NoviceToDotNet Avatar asked Oct 01 '10 09:10

NoviceToDotNet


4 Answers

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.

like image 173
Øyvind Bråthen Avatar answered Oct 29 '22 22:10

Øyvind Bråthen


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)

like image 43
sumit_programmer Avatar answered Oct 29 '22 22:10

sumit_programmer


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).

like image 11
Oded Avatar answered Oct 29 '22 21:10

Oded


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).

like image 10
Dhananjay Avatar answered Oct 29 '22 21:10

Dhananjay