Method Overloading Vs Method Overriding in Java
Method Overloading Vs Method Overriding in Java
There are the following differences between method overloading and method
overriding in Java. They are as follows:
1. Definition:
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.
5. Class:
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.
10. Polymorphism:
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
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.