Virtual / Overridable methods are those which may or may not implemented in the derived class,but they must have some implementation in the base class.
When a derived class method overrides its baseclass method the signature should be same as in the base class, this includes the modifier, parameters and the return type of the method, any modifications in the derived class will lead to a compilation error.
If method is declared virtual in the base class then it is not mandatory to override that function in the derived class. In fact that method need not be a part of the derived class at all. However if the method with the same signature is present in the derived class then it needs clarify whether it is overriding the base class member or not.
a)If the method in the derived class is ment to override the base class member then it needs to have the keyword 'override'
b)If the method in the derived class is not ment to override the base class member then it needs to have the keyword new [Shadows in vb.net] , this is called 'Hiding the Base Class Member'
c)If the method in the derived class has the same name as in the base class but if it has a different signature then it is not required to state either override or new since this method neither overrides not hides the base class method.
These options are not available for an Abstract method, a method declared Abstract in the Base Class must ob overridden in the derived class, it cannot hide the base class member using the 'new' keyword.
No comments:
Post a Comment