⏱️ 9 min read • Beginner Level • Lesson 35
Method overloading in Java means defining multiple methods with the same name in the same class, but with different parameter lists. It helps make code readable and allows one method name to perform similar tasks with different inputs.
Before learning method overloading, you should understand Java Methods and Java Method Parameters. After this lesson, continue with Java Recursion.
Method overloading allows a class to have more than one method with the same name, but each method must have a different parameter list.
void add(int a, int b) { }
void add(int a, int b, int c) { }
void add(double a, double b) { }
All three methods are named add, but their parameter lists are different.
Method overloading is commonly used when the same operation needs to work with different types or amounts of data.
add() methods:
add(int, int)add(int, int, int)add(double, double)
Users perform the same operation (addition), but with different inputs. Instead of creating methods such as
addTwoNumbers(),
addThreeNumbers(),
and
addDecimalNumbers(),
Java allows the same method name to be reused.
To overload a method, the method name must be the same, but the parameter list must be different.
| Allowed? | Overloading Rule | Example |
|---|---|---|
| Yes | Change number of parameters | add(int, int), add(int, int, int) |
| Yes | Change data type of parameters | add(int, int), add(double, double) |
| Yes | Change order of parameters | show(String, int), show(int, String) |
| No | Only changing return type | int sum(), double sum() |
are not overloaded.void show(int age)andvoid show(int marks)
Method Call ➜ Compiler Checks Method Name ➜ Matches Parameter List ➜ Selects Best Matching Method ➜ Executes Method
In this type, methods have the same name but different number of parameters.
public class OverloadByNumber {
static int add(int a, int b) {
return a + b;
}
static int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
System.out.println(add(10, 20));
System.out.println(add(10, 20, 30));
}
}
Output:
30 60
In this type, methods have the same name and same number of parameters, but parameter data types are different.
public class OverloadByType {
static int add(int a, int b) {
return a + b;
}
static double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(10, 20));
System.out.println(add(10.5, 20.5));
}
}
Output:
30 31.0
Method overloading can also be done by changing the order of parameters when the data types are different.
public class OverloadByOrder {
static void show(String name, int age) {
System.out.println("Name: " + name + ", Age: " + age);
}
static void show(int age, String name) {
System.out.println("Age: " + age + ", Name: " + name);
}
public static void main(String[] args) {
show("Ayan", 20);
show(22, "Rahul");
}
}
Output:
Name: Ayan, Age: 20 Age: 22, Name: Rahul
No, Java does not allow method overloading by changing only the return type. The parameter list must be different.
public class InvalidReturnTypeOverloading {
static int getValue() {
return 10;
}
// Error: method getValue() is already defined
static double getValue() {
return 10.5;
}
}
If an exact matching method is not found, Java may promote a smaller data type to a larger compatible data type.
For example, int can be promoted to long, float, or double.
public class TypePromotionExample {
static void show(double number) {
System.out.println("double method: " + number);
}
public static void main(String[] args) {
show(10); // int is promoted to double
}
}
Output:
double method: 10.0
Yes, static methods can be overloaded in Java if their parameter lists are different.
public class StaticMethodOverloading {
static void print(int number) {
System.out.println("Number: " + number);
}
static void print(String text) {
System.out.println("Text: " + text);
}
public static void main(String[] args) {
print(100);
print("Java");
}
}
Output:
Number: 100 Text: Java
Yes, the main() method can be overloaded in Java. However, JVM starts execution only from
public static void main(String[] args).
public class MainMethodOverloading {
public static void main(String[] args) {
System.out.println("Original main method");
main(10);
}
public static void main(int number) {
System.out.println("Overloaded main method: " + number);
}
}
Output:
Original main method Overloaded main method: 10
Constructors can also be overloaded. Constructor overloading means creating multiple constructors in the same class with different parameter lists.
class Student {
String name;
int age;
Student() {
name = "Unknown";
age = 0;
}
Student(String studentName) {
name = studentName;
age = 18;
}
Student(String studentName, int studentAge) {
name = studentName;
age = studentAge;
}
void showDetails() {
System.out.println(name + " - " + age);
}
}
public class ConstructorOverloadingExample {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student("Ayan");
Student s3 = new Student("Rahul", 22);
s1.showDetails();
s2.showDetails();
s3.showDetails();
}
}
Output:
Unknown - 0 Ayan - 18 Rahul - 22
| Method Overloading | Method Overriding |
|---|---|
| Same method name with different parameters. | Same method signature in parent and child class. |
| Can happen in the same class. | Requires inheritance. |
| Compile-time polymorphism. | Runtime polymorphism. |
| Return type alone cannot overload a method. | Return type must be same or covariant. |
This example shows overloading by number of parameters, data type of parameters, and order of parameters.
public class MethodOverloadingCompleteExample {
static int calculate(int a, int b) {
return a + b;
}
static int calculate(int a, int b, int c) { // Overloaded by number of parameters
return a + b + c;
}
static double calculate(double a, double b) { // Overloaded by data type
return a + b;
}
static void calculate(String operation, int number) {
System.out.println(operation + ": " + number);
}
static void calculate(int number, String operation) { // Overloaded by order
System.out.println(number + " - " + operation);
}
public static void main(String[] args) {
System.out.println(calculate(10, 20));
System.out.println(calculate(10, 20, 30));
System.out.println(calculate(10.5, 20.5));
calculate("Total", 100);
calculate(200, "Marks");
}
}
Output:
30 60 31.0 Total: 100 200 - Marks
calculate().🧠 Test your understanding with a quick quiz
Topic: Method-overloading | Language: Java
public class Test {
static int add(int a, int b) {
return a + b;
}
static int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
System.out.println(add(10, 20));
}
}public class Test {
static void show(double number) {
System.out.println(number);
}
public static void main(String[] args) {
show(10);
}
}public class Test {
static int add(int a, int b) {
return a + b;
}
static double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(10.5, 20.5));
}
}🎉 Great job! Continue learning Java step by step.