Method Overriding and Method Overloading
When it comes to Polymorphism Difference between Overloading and Overriding is the most frequently asked question in job interviews.
When it comes to Polymorphism Difference between Overloading and Overriding is the most frequently asked question in job interviews.
Lets quickly understand the difference. Overloading always happens in single class and overriding always occur between two classes where inheritance exists.
Method Overloading
Method overloading is the concept of using same method name within same class but with different parameters.
Compiler knows at compile time that which method will be called in case of overloading
Example : Suppose we have two methods of same name ‘Add’ but one method accepts two parameters as an input and other method takes three parameters as an input.
Method Overriding
Method overriding is the concept where child class implement a method with its own way ignoring how parent has provided its implementation.
Compiler does not know that which method will be called in case of overriding , it is decided at run time that which method will be called
Example : Suppose we have a class ‘Animal’ that have a method of ‘Sound’ which provides its implementation but when a class ‘Dog’ inherits from ‘Animal’ class it provides its implementation in its own way.
C# Code Demo of Overriding and Overloading
I have attached demo code using C# language above.
If you like my work you can help me in growing through following ways