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.
Java
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));
}
}
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.
Java
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);
}
}
OutputProduct 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.
Java
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));
}
}
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.
Java
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");
}
}
OutputName: 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).
Java
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
}
}