Method overloading in Java allows a class to have multiple methods with the same name but different parameter lists. It is a form of compile-time polymorphism, because the compiler determines which overloaded method to call based on the arguments passed.
- Their parameter list must be different.
- Parameters can differ in number, type, or order.
- Changing only the return type does not create method overloading.
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
double add(double a, double b) {
return a + b;
}
}
public class Geeks {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(10, 20));
System.out.println(calc.add(10, 20, 30));
System.out.println(calc.add(10.5, 20.5));
}
}
Output
30 60 31.0
Explanation: The Calculator class has three add() methods with different parameter lists. The first accepts two int values, the second accepts three int values, and the third accepts two double values. When add() is called, the compiler determines which method to execute based on the number and type of arguments passed.
Ways of method overloading
Method overloading can be achieved mainly in the following ways:
1. Changing the Number of Parameters
Method overloading can be achieved by changing the number of parameters when passing to different methods.
import java.io.*;
class Product{
// Multiplying two integer values
public int multiply(int a, int b){
int prod = a * b;
return prod;
}
// Multiplying three integer values
public int multiply(int a, int b, int c){
int prod = a * b * c;
return prod;
}
}
class Geeks{
public static void main(String[] args)
{
Product ob = new Product();
// Calling method to Multiply 2 numbers
int prod1 = ob.multiply(1, 2);
// Printing Product of 2 numbers
System.out.println(
"Product of the two integer value: " + prod1);
// Calling method to multiply 3 numbers
int prod2 = ob.multiply(1, 2, 3);
// Printing product of 3 numbers
System.out.println(
"Product of the three integer value: " + prod2);
}
}
Output
Product of the two integer value: 2 Product of the three integer value: 6
Explanation:
- Two methods have the same name but different number of parameters.
- Compiler selects the correct method based on how many arguments are passed.
2. Changing Data Types of Parameters
In many cases, methods can be considered overloaded if they have the same name but have different parameter types, methods are considered to be overloaded.
class Product{
public int prod(int a, int b, int c){
return a * b * c;
}
public double prod(double a, double b, double c){
return a * b * c;
}
}
public class Geeks {
public static void main(String[] args){
Product p = new Product();
System.out.println(p.prod(1, 2, 3));
System.out.println(p.prod(1.0, 2.0, 3.0));
}
}
Output
6 6.0
Explanation:
- Methods differ in parameter types (int vs double).
- Compiler matches the method based on the exact data type of arguments.
3. Changing the Order of Parameters
Method overloading can also be implemented by rearranging the parameters of two or more overloaded methods.
class Student {
public void studentId(String name, int rollNo){
System.out.println("Name: " + name
+ ", Roll-No: " + rollNo);
}
public void studentId(int rollNo, String name){
System.out.println("Roll-No: " + rollNo
+ ", Name: " + name);
}
}
public class Geeks{
public static void main(String[] args){
Student s = new Student();
s.studentId("Sweta", 1);
s.studentId(2, "Gudly");
}
}
Output
Name: Sweta, Roll-No: 1 Roll-No: 2, Name: Gudly
Explanation:
- Methods have the same name but parameter order is different.
- Compiler identifies which method to call based on sequence of arguments.
Note : There can be a hybrid overloading also where number of parameters, type of parameters and order of parameters cam change in any combination.
What if the Exact Prototype Does Not Match?
When an exact overloaded method is not available, Java may perform method invocation conversions, including certain primitive widening conversions, to find a compatible method. Java tries type promotion in overloading
- Convert to a higher type in the same hierarchy (e.g., byte -> int).
- Convert to the next higher hierarchy if needed (e.g., int -> float).
class Demo{
public void show(int x){
System.out.println("In int: " + x);
}
public void show(String s){
System.out.println("In String: " + s);
}
public void show(byte b){
System.out.println("In byte: " + b);
}
}
public class UseDemo {
public static void main(String[] args) {
Demo obj = new Demo();
obj.show((byte) 25);
obj.show("hello");
obj.show(250);
obj.show('A');
// obj.show(7.5); // Error: no suitable method for double
}
}
Output
In byte: 25 In String: hello In int: 250 In int: 65
Explanation:
- obj.show((byte) 25) calls show(byte) because the argument is explicitly a byte.
- obj.show("hello") calls show(String) because the argument is a String.
- obj.show(250) calls show(int) because an integer literal such as 250 has type int by default.
- obj.show('A') calls show(int) because a char can be widened to int.
- obj.show(7.5) causes a compilation error because 7.5 is a double, and there is no suitable show(double) method. Java does not perform a narrowing conversion from double to int or byte automatically.
Overloading vs Overriding
| Method Overloading | Method Overriding |
|---|---|
| Same method name, different parameters | Same method signature in parent and child |
| Usually occurs within the same class | Requires inheritance |
| Compile-time polymorphism | Runtime polymorphism |
| Compiler selects the method | JVM selects the overridden instance method at runtime |
| Parameters must differ | Parameters must match |