Difference between Method Overriding and Method Overloading in Java

In this article, we will list the difference between method overriding and method overloading in Java

Before moving ahead with the differences, read the detailed concepts about method overloading and method overriding in the following articles

Let us detail out difference between method overriding v/s method overloading in tabular form below

Method Overloading v/s Method Overriding

 
Sr. No.
 
 
Method Overloading
 
 
Method Overriding
 
1 If any class contains multiple methods with exactly same name but with different input parameter list then it is known as method overloading If a sub class has a same instance method with same method signature as that of the super class’s method then it is said to be method overriding
2 Method name should be same but with different number of input parameters or data-type of input parameters (includes order/sequence of input parameters) Method signature should be same both in super class as well as in sub class (including access modifier, return type & exception of method signature)
3 Method signature has to be different Method signature should be same
4 Input parameter lists has to be different Input parameter lists should be same even their data-types and order/sequence should same
5 Overloading happens in the same class (just one class) Overriding happens in 2 or more classes through inheritance concept
6 This provides multiple implementation version with same method name in same class This provides specific implementation in sub class when extending from super class’s more general implementation
7 This is resolved at compile-time therefore it is also known as compile-time polymorphism This is resolved at run-time therefore it is also known as run-time polymorphism
8 Sometimes, this is referred as static binding as method call resolves during compilation And this is referred as dynamic binding as method calls resolves during execution
9 Method overloading increases the readability of the program This is use to provides specific implementation in the extending class
10 Return type can be same or different in case method overloading as it is doesn’t get counted Return type has to be same from that of super class’s return type (or else it should sub-class or sub-type of super class’s return type)
This is called co-variant return type
11 Overloading gives better performance as it is resolved during compile-time Overriding performance is slightly on the lower side as compared to overloading
12 Non-access modifiers like static or final aren’t get accounted in method overloading

Therefore, overloaded methods can have static, final keyword in method signature

Final methods cannot be overridden (this is inheritance concept)

Static methods cannot be overridden, rather it can be re-declared in the sub class

13 Also, access modifiers like private aren’t get accounted in method overloading Private methods cannot be overridden (again, this is inheritance concept)
14 Read method overloading rules  Read method overriding rules

 

Example on Method Overloading

package in.bench.resources.java.overload;

public class TestJavaOverload {

	void add(int num1, float num2) {
		System.out.println("The summation of 2 numbers : " + (num1 + num2));
	}

	void add(int num1, float num2, int num3) {
		System.out.println("The summation of 3 numbers : " + (num1 + num2 + num3));
	}

	public static void main(String args[]) {

		TestJavaOverload t1 = new TestJavaOverload();
		t1.add(12, 16f); // invoking 1st method with 2 arguments
		t1.add(10, 20f, 30); // invoking 1st method with 3 arguments
	}
}

Output:

The summation of 2 numbers : 28.0
The summation of 3 numbers : 60.0

Example on Method Overriding

Super class

package in.bench.resources.method.overriding;

public class Shape {

	void draw() throws Exception {
		System.out.println("Super class >> Shape : draw() method");
	}
}

 

Sub class

package in.bench.resources.method.overriding;

public class Circle extends Shape {

	@Override
	protected void draw() throws Exception {
		System.out.println("Sub class >> Circle : draw() method");
	}
}

 

Read also: