Link:-
https://www.codeproject.com/Articles/18734/Method-Overriding-in-C
https://www.geeksforgeeks.org/c-sharp-method-overriding/
Runtime polymorphism(Overriding):
- In this type of polymorphism, compiler identifies which polymorphism form it has to take and execute at runtime but not at compile time is called as runtime polymorphism or late binding . Example of early binding is Method Overriding . The Method Overriding means having two methods with same name and same signature, one method in base class and other method in derived class. It must require changing the behavior of the base class methods in derived class to use its functionality differently.
– Advantage: It has flexibility to adjust object types at runtime.
– Disadvantage: Execution will be slow as compiler has to get the information about the method to execute at runtime.
We need to use either virtual methods or abstract method to allow the derived class to override a method of the base class.
Example – [Method Overriding by using virtual method]
In above example, when you run the program, at compile time the type of objBase _is _Base _but it will still call the child class’s override method because at the _runtime , the type of the objBase _object refers to is _Child
Example – [Method Overriding by using abstract method]
In above example, when you run the program, at compile time the type of objBase _is _Base _but it will still call the child class’s override method because at the _runtime , the type of the objBase _object refers to is _Child
.