Reputation: 4269
When you override a method, you shouldn't change the behaviour of the method, you just specialise it. Therefore you have to call base.MyVirtualMethod()
in the overridden method and add the specialisation code.
But I'm always wondering when I have to call the base.MyVirtualMethod()
. Or from another point of view, how do I write my virtual method? Should I expect the user will call it as the first or the last thing the overridden method does?
public class Parent
{
public virtual void MyMethod(){ /* Some code*/ }
}
public class Child : Parent
{
public override void MyMethod()
{
/* Does my code goes here? */
base.MyMethod();
/* Or does my code goes here? */
}
}
Upvotes: 2
Views: 663
Reputation: 13198
The base call does not have to be present. You can specify in the documentation whether the base call should be before other code, after other code, both, or neither (absent), and exactly what the nature of the other code should be. This will all depend on what you are trying to accomplish.
If you find that the best place for the additional code would really be somewhere inside the base call, then that means the base method should be split into two or more methods.
Upvotes: 2
Reputation: 46008
Therefore you have to call base.MyVirtualMethod() in the overridden method and add the specialisation code.
That is not always true - there are cases when you don't want to do in the derived class what the superclass is doing so you don't want to call base
.
If you want to extent the base behavior you place your code before or after base
call, depending on the actual problem. There is no rule 'always call base before your code'.
Upvotes: 4
Reputation: 331
I generally call the base method first to assure that any required initialization has already happened and that my code actually overrides the base behavior, instead of the other way around. Obviously this depends on the specific situation though, there may very well be occasions where you know your code needs to run first.
Upvotes: 1
Reputation: 77
To answer your question accurately: don't override it :) When you override it, you will change the behaviour. Most of the times I put new code below the calling of the base method, since it does perform this base behaviour and then some more additional behaviour. However this is not set in stone, and really depends on your needs.
Upvotes: 1
Reputation: 998
The answer is, as with many questions, "It depends".
Assume that you're extending class which writes some data to a file, and the extension needs to add more data at the end of the file (SimpleDataFile.writeFile()
extended by ExtendedDataFile.writeFile()
): in such a scenario, you would add your code after the call to the base method.
Now, assume your extension adds a pre-processing facility, maybe adding color to the base file output (SimpleDataFile.writeFile()
extended by FancyDataFile.writeFile()
): in such a scenario you would realistically act before anything is sent to the file, and your code would end up before the base call.
Upvotes: 1