2
Reply

What is method overriding in c#

kalpa serashiya

kalpa serashiya

13y
13.3k
0
Reply

    Method overriding in C# is a feature like the virtual function in C++. Method overriding is a feature that allows you to invoke functions that have the same signatures that belongs to different classes in the same hierarchy of inheritance using the base class reference. C# makes use of two keywords: virtual and overrides to accomplish Method overriding.

     

     

    If a class contains a  virtual method, then a subclass can override that method by declaring it again, exactly as before, but using the 'override' keyword in place of 'virtual'. The subclass must then provide its own implementation of the method. 

    If the method is then called via a base class variable, it is the runtime type of that variable which determines which version of the method is called i.e. if the base class variable actually contains a reference to the subclass, then it's the subclass's version which is called.

    Similarly, if an abstract class contains an abstract method, then a concrete subclass must override the method by declaring it again, exactly as before, but using the 'override' keyword in place of 'abstract'. The subclass must then provide its own implementation of the method.