Last Updated on August 1, 2020 by Scientech Easy
In this tutorial, we will discuss the top 10 differences between overloading and overriding in Java.
What is the difference between method overloading and method overriding in Java? is the most valuable, very important, and repeatedly asked question in any Java technical interview.
Interviewer always hopes three to four differences from your answer. So, let’s start with a basic definition.
Method Overloading vs Method Overriding
There are the following differences between method overloading and method overriding in Java. They are as follows:
a. When a class has more than one method having the same name but different in parameters, it is called method overloading in Java.
b. When the method of superclass is overridden in subclass to provide more specific implementation, it is called method overriding in Java.
2. Argument type:
a. Argument type must be different (at least order) in overloading.
b. In overriding, argument type must be the same (including order).
3. Method signature:
a. The signature of the overloaded method must be different.
b. The signature of the overriding method must be the same.
4. Return type:
a. In method overloading, the return type can be the same or different.
b. In method overriding, the return type must be the same until Java 1.4 version but Java 1.5 onwards, method overriding can be done by changing the covariant return type.
a. Method overloading is generally performed in the same class.
b. Method overriding is performed in two classes through inheritance (Is-A relationship).
6. Private/static/final method:
a. Private, static, and final method can be overloaded in Java.
b. Overriding concept is not applicable to private, static, and final method. Private, static, and final method can be overridden in Java.
7. Access modifiers:
a. In overloading, access modifiers can be anything or different.
b. In overriding, subclass method’s access modifier must be the same or higher than superclass method access modifier i.e we cannot reduce the visibility subclass method while overriding.
8. Throws clause:
a. Exception thrown can be anything in the overloading concept.
b. In case of method overriding, if child class method throws any checked exception compulsory parent class method should throw the same checked exception are its parent otherwise we will get compile-time error but there is no restriction for an unchecked exception.
9. Method resolution:
a. Method resolution in overloading always takes care by the compiler based on reference type.
b. Method resolution in overriding always takes care by JVM based on runtime object.
a. Method overloading is also known as compile-time polymorphism, static polymorphism, or early binding.
b. Method overriding is also known as runtime polymorphism, dynamic polymorphism, or late binding.
Overloading vs Overriding in Tabular Form
A list of differences between overloading and overriding in Java is given below for quick revision in tabular form.
|1||Argument type||Must be different (at least order).||Must be the same (including order).|
|2||Method signatures||Must be different.||Must be the same.|
|3||Return type||Same or different.||Must be the same until Java 1.4 version only. Java 1.5 onwards, Covariant return type is allowed.|
|4||Class||Generally performed in the same class.||Performed in two classes through Inheritance (Is-A relationship).|
|5||Private/Static/Final method||Can be overloaded.||Cannot be overridden.|
|6||Access modifiers||Anything or different.||Subclass method’s access modifier must be same or higher than superclass method access modifier.|
|7||Throws clause||Anything||If child class method throws any checked exception compulsory parent class method should throw the same exception is its parent otherwise we will get compile-time error but there is no restriction for an unchecked exception.|
|8||Method resolution||Always take care by java compiler based on reference type.||Always take care by JVM based on runtime object.|
|9||Polymorphism||Also known as compile-time polymorphism, static polymorphism, or early binding.||Also known as runtime polymorphism, dynamic polymorphism, or late binding.|
Hope that this tutorial has covered all the important points to differentiate between method overloading and method overriding in Java. I hope that you will have understood the basic differences and enjoyed it
Thanks for reading!!!