Java – Difference between Method Overriding and Overloading ?

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

1. Method Overloading v/s Method Overriding

 
Sr. No.
 
 
Method Overloading
 
 
Method Overriding
 
1If any class contains multiple methods with exactly same name but with different input parameter list then it is known as method overloadingIf 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
2Method 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)
3Method signature has to be differentMethod signature should be same
4Input parameter lists has to be differentInput parameter lists should be same even their data-types and order/sequence should same
5Overloading happens in the same class (just one class)Overriding happens in 2 or more classes through inheritance concept
6This provides multiple implementation version with same method name in same classThis provides specific implementation in sub class when extending from super class’s more general implementation
7This is resolved at compile-time therefore it is also known as compile-time polymorphismThis is resolved at run-time therefore it is also known as run-time polymorphism
8Sometimes, this is referred as static binding as method call resolves during compilationAnd this is referred as dynamic binding as method calls resolves during execution
9Method overloading increases the readability of the programThis is use to provides specific implementation in the extending class
10Return type can be same or different in case method overloading as it is doesn’t get countedReturn 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
11Overloading gives better performance as it is resolved during compile-timeOverriding performance is slightly on the lower side as compared to overloading
12Non-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

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

2. Example on Method Overloading

TestJavaOverload.java

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

3. Example on Method Overriding

Super class – Shape.java

package in.bench.resources.method.overriding;

public class Shape {

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

Sub class – Circle.java

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");
	}
}

Related Articles:

Happy Coding !!
Happy Learning !!

Java - Constructor with example
Java - Interview Question and Answers on Method Overloading