Page 2 of 2
If you are an expert C# programmer you are probably jumping up and down saying "where is the virtual and the override?"
Yes it really is this simple, but don't underestimate the scope for confusion here. The fact is that if you look at the warning message generated by this program, it suggests that you might want to add "new" to the program which is probably not what is wanted. The simple fact that C# has three keywords associated with redefining inherited methods is an indication that it is complicated.
The first thing to realize is that methods have to be declared as virtual if they are going to be overridden in derived classes in a polymorphic way. Any method in a base class that isn't marked as virtual are not intended to be used polymorphically.
So the first rule is that you have to declare any method that you want to use polymorphically as virtual.
The complications now arise because C# tries to protect you from a type of error that you might not think you need much protection from. Suppose you add a method to a derived class then this method is exclusive to your derived class and any classes that you derive from it, i.e. it isn't available for use in the base class.
Now suppose the base class implements a method with the same name and signature - without you knowing anything about it. Now your method seems to be a redefinition of the base classes new method - but of course it isn't it is a new method.
What should happen next depends on how the method is declared in the base class.
If it is introduced without the keyword
then the programmer did not intend the method to be overridden polymorphically. In this case this is what should happen and the base method should be used if the reference is of base type, even if the object is of derived type. This is exactly what happened in our puzzle example.
To make it clear that both sized of the deal understand the derived method is supposed to be declared as
in the derived class - so acknowledging that this is what is intended. The "new" method plays no part in the inheritance hierarchy and it doesn't override the base method. It is simply used by references to the derived type when it is said to "hide" the base type.
Now consider what should happen is the new method in the base class is declared as virtual.
In this case the base class is allowing the method to be overridden and it should take part in the inheritance hierarchy and polymorphism.
If the derived class's method declares itself to be "new" or doesn't add a declaration when it is assumed to be "new" then it shouldn't override the base class's method. Once again we get the base class method called irrespective of the type of the object. So no change.
If, however, the method in the derived class is declared as
then it is playing the hierarchy game and will engage in polymorphism.
So the solution to the puzzle is that the base class method has to be declared as virtual - indicating that it can be overridden and the derived class method has to be declared as override - indicating that it means to override the base class method.
Complicated? Here is a summary of the effect of each possible declaration:
Notice that if the derived class attempts to override a base class method that isn't intended to be overridden then you get an error - this is the error that all of this is supposed to catch.
To avoid this sort of problem you have to be very clear when defining classes and methods which methods you are going to allow to be overridden.
- Declare any method that you want to allow to take part in polymorphism and be overridden as virtual in the base class.
- Declare any method that you believe is going to override a base class method as override.
The key however is to understand quite clearly that methods don't play the polymorphism game unless you tell them to both in the base and the derived class.
To be informed about new articles on I Programmer, subscribe to the RSS feed, follow us on Google+, Twitter, Linkedin or Facebook or sign up for our weekly newsletter.