Method Overloading and Method Overriding are both the techniques used to implement FUNCTIONAL POLYMORPHISM
Method Overloading (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.
Method Overloading forms compile-time polymorphism.
Example :
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 (RUN TIME 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.
Example :
Difference between method overloading and method overriding
Method Overloading (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.
Method Overloading forms compile-time polymorphism.
Example :
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 (RUN TIME 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.
Example :
Difference between method overloading and method overriding
Overloading | Overriding |
Having same method name with different Signatures. | Methods name and signatures must be same. |
Overloading is the concept of compile time polymorphism | Overriding is the concept of runtime polymorphism |
Two functions having same name and return type, but with different type and/or number of arguments is called as Overloading | When a function of base class is re-defined in the derived class called as Overriding |
It doesn't need inheritance. | It needs inheritance. |
Method can have different data types | Method should have same data type. |
Method can be different access specifies | Method should be public. |