Method Overloading :
Method Overloading is a feature in Java that allows a class to have two or more methods with the same name but different parameter lists (different number of parameters, different types of parameters, or a different sequence of parameter types).
It is similar to constructor overloading, where a class can have multiple constructors with different argument lists.
Ways to Achieve Method Overloading
Method overloading in Java can be done in three ways:
- By changing the number of parameters
- By changing the data types of parameters
- By changing the sequence of parameter data types
1.By Changing the Number of Parameters :
package com.tutorial4u;
class Addition {
public void sum(int a, int b) {
int sum = a+b;
System.out.println("sum of a and b :"+sum);
}
public void sum(int a , int b, int c) {
int sum = a+b+c;
System.out.println("sum of a, b, and c : "+sum);
}
}
public class OverloadingExample {
public static void main(String[] args) {
Addition add = new Addition();
add.sum(10, 20);
add.sum(10, 20, 30);
}
}
Output :
Sum of a and b : 30
Sum of a, b, and c : 60
2. By Changing the Data Types of Parameters
package com.tutorial4u;
class Addition {
public void sum(int a, int b) {
int sum = a+b;
System.out.println("sum of a and b :"+sum);
}
public void sum(double a , double b) {
double sum = a+b;
System.out.println("sum of a, b, and c : "+sum);
}
}
public class OverloadingExample {
public static void main(String[] args) {
Addition add = new Addition();
add.sum(10, 20);
add.sum(10.5, 20.5);
}
}
Output :
Sum of a and b : 30
Sum of a, b, and c : 31.0
3.By Changing the Sequence of Data Types
package com.tutorial4u;
class Tutorial {
public void display(int a, char c) {
System.out.println("I am the first definition of method");
}
public void display(char c , int b) {
System.out.println("I am the second definition of method");
}
}
public class OverloadingExample {
public static void main(String[] args) {
Tutorial add = new Tutorial();
add.display(10, 'a');
add.display('b', 20);
}
}
Output :
I am the first definition of method
I am the second definition of method
Special Cases in Method Overloading :
Case 1: what happen when method signature is same but return type is different ?
package com.tutorial4u;
class Addition {
public int sum(int a, int b) {
int sum = a+b;
return sum;
}
public double sum(int a , int b) {// Error!
double sum = a+b;
return sum;
}
}
public class OverloadingExample {
public static void main(String[] args) {
Addition add = new Addition();
add.sum(10, 20);
add.sum(10, 20);
}
}
Answer : It will through a compilation error
Duplicate method sum(int, int) in type Addition
Note :👉 Java does not allow method overloading only by changing the return type.
Case 2: Can we overload java main() method?
Answer : Yes. We can overload the
main
method any number of times.However, the JVM always calls the main(String[] args)
method to start program execution.
Example :
class OverloadingExample{
public static void main(String[] args){
System.out.println("method 1");
}
public static void main(String args){
System.out.println("method2");
}
public static void main(){
System.out.println("method3");
}
}
Output :
method 1
Important Points about Method Overloading :
- Java does not allow overloading by just changing the return type.
- Private methods can be overloaded.
- Final methods can be overloaded.
- Static methods can be overloaded.
- The
main()
method can also be overloaded, but the JVM always looks formain(String[] args)
.