Method overloading and overriding are very important concepts of Java programming. Below we will see the definitions first and than we will compare both concepts in details.
Method Overloading - More than one method with same name but different argument list in a class is called method overloading. Return type can be same or different in method overloading.
Method Overriding - Method overriding is a concepts that basically works when there is a parent-child relationship between two classes. Method overriding is used when we want to assign specific implementation to a method of parent class in child class. So defining a method with same name and argument list as parent class in child class is called method overriding. In method overriding the return type must be same or covariant of the parent class class method's return type.
Difference between method overloading and method overriding -
- Method overloading works in same class, but method overriding works with parent and child classes.
- In method overloading argument list must be changed, but in method overriding argument list must be same.
- Return type in method overloading can be different, but return type in method overriding must be same or covariant of overridden method.
- Access modifier do not effect in method overloading, but we can not make the overridden method more restrictive in method overriding. e.g.. in overriding a public overridden method of parent class can not have protected or private modifier in child class.
- Method overloading happens at compile-time(static-binding), while method overriding takes place at run-time(dynamic-binding).
- Static methods can be overloaded, but we can not override static methods.
- Private and final methods can be overloaded, but we can not override private and final methods.
- Overloaded methods are faster than overrided methods, because overloaded methods binding happens at compile-time while binding of overrided methods take place at run-time.
These are the basic differences between method overloading and overriding. If you have any doubts about the same please put in comments, I will try to resolve ASAP.
Check below links also-
Difference between Interface and Abstract class
Difference between Abstraction and Encapsulation
Difference between Interface and Abstract class
Difference between Abstraction and Encapsulation
Difference between Comparable and Comparator in Java
***********************Keep studying***********************
No comments:
Post a Comment