Last updated on February 27th, 2024
In this topic, we will learn what are the main differences between method overloading and method overriding in Java. So let’s begin with a simple way to understand the differences between method overloading and method overriding.
Method Overloading vs Method Overriding in Java
There are various differences between method overloading and method overriding in Java. These are mentioned below here:
1. Definition
If a class is having more than one method with the same name but different arguments known as method overloading in Java.
If the Child class is satisfied with the method of the Parent class then it is provided to implementation of the method in its own way is known as method overriding in Java.
2. Argument Type
• In the method, the overloading argument type must be different(at least in order).
• In the method, the overriding argument type must be the same (including order).
3. Method Signature
• In the method, the overloading method signature must be different.
• In the method, the overriding method signature must be the same.
4. Return Type
• In the method, the overloading return type can be the same or different
• In the method, the overriding return must be the same(till Java 1.4 version) or covariant(Java 1.5 version onwards)
5. Class
• Method overloading is usually performed within the same class.
• Method overriding is performed within the Parent class and Child class(via Inheritance).
6. Private, static, final methods
• Private, static and final methods can overload.
• Private, static and final methods cannot be overriding.
7. Access Modifier
• In the method overloading there are no restrictions in the access modifier.
• In the method overriding weakening/reducing access modifier is not allowed.
8. Throws Clause
• In the method overloading there are no restrictions for Exception throws.
• In the method overriding, if a Child class method throws any checked exception then the Parent class method mandatory should throw the same checked exception or its parent. But there is no restriction for unchecked exceptions.
9. Method Resolution
• In method overloading, method resolution always takes care of the compiler-based (reference type).
• In method overriding, method resolution always takes care of by the JVM-based (runtime object).
10. Polymorphism
• The method overloading is also known as compile time polymorphism or static/early binding.
• The method overriding is also known as runtime polymorphism or dynamic/late binding.
Overloading vs Overriding in Java in the Tabular Form
There is a list of the main differences between overloading and overriding below table here:
Property | Overloading | Overriding |
1) Method Name | Must be same | Must be same |
2) Argument Type | It must be different(at least in order) | It must be different(at least in order) |
3) Method signature | Must be different | Must be same |
4) Return types | No restrictions | It must be the same till 1.4v but from 1.5v onwards co-variant return types are also allowed |
5) private, static, final methods | These methods can be overloaded | These methods cannot be overridden |
6) Access modifiers | No restrictions | Weakening/reducing is not allowed |
7) Throws clause | No restriction | If the child class method throws any checked exception mandatory parent class method should throw the same checked exceptions or its parent but no restrictions for unchecked exceptions |
8) Method Resolution | Compiler based on the referenced type | JVM based on runtime object |
9) Polymorphism | It is also called Compiler time polymorphism or static or early binding | It is also called Runtime polymorphism or dynamic or late binding |
Conclusion
In this topic, we learnt the differences between overloading and overriding in Java.