OOPS Series - Difference between method overloading and overriding

method overloading
Method Overloading and Method Overriding are both the techniques used to implement POLYMORPHISM  

Method Overloading -
Method overloading known as compile time polymorphism, Method Overloading means having two or more methods with the same name but with different signature(different parameters list and different type of parameters) in same class or in different classes.

Lets see a example to simplify Method overloading

We will create a method that will calculate the square of a given number having same name but different data type.In this program we have use different methods having same name but different parameters this is called overloading.


method overriding

Method overriding -
Method overriding known as runtime polymorphism , Method overriding means having two methods with same name and same signature, one method in base class and other method in derived class.

A subclass inherits methods from a base class. Sometimes, it is necessary for the subclass to modify the methods defined in the base class. This is referred to as method overriding.

This can be achieved by using the virtual and override keywords. we have to use the virtual keyword for the method which in base class and override keyword  for the method in subclass.

By default functions are not virtual in C# and so you need to write “virtual” explicitly.


No comments:

Post a Comment