Java Method Overloading
Multiple methods with the same name
🔄 What is Method Overloading?
Method overloading allows multiple methods with the same name but different parameters. Java chooses the correct method based on the arguments you provide when calling it.
// Same method name, different parameters
public static void print(String text) { }
public static void print(int number) { }
Java automatically picks the right method:
print("Hello") → calls String version
print(42) → calls int version
Key Overloading Concepts
Same Name
All methods have identical names
void calculate() { }
void calculate(int x) { }
Different Parameters
Methods differ by parameter count/type
void method(int a) { }
void method(String b) { }
Automatic Selection
Java picks the right method automatically
method(5); // calls int version
method("hi"); // calls String version
Code Reuse
Same functionality, different inputs
add(2, 3);
add(1.5, 2.7);
🔹 Basic Overloading Example
Multiple methods with the same name but different parameter types:
public class OverloadingExample {
// Method 1: Print string
public static void display(String message) {
System.out.println("String: " + message);
}
// Method 2: Print integer
public static void display(int number) {
System.out.println("Integer: " + number);
}
// Method 3: Print double
public static void display(double decimal) {
System.out.println("Double: " + decimal);
}
public static void main(String[] args) {
display("Hello World"); // Calls String version
display(42); // Calls int version
display(3.14); // Calls double version
}
}
Output:
String: Hello World
Integer: 42
Double: 3.14
🔹 Different Number of Parameters
Overloading by changing the number of parameters:
public class Calculator {
// Add two numbers
public static int add(int a, int b) {
return a + b;
}
// Add three numbers
public static int add(int a, int b, int c) {
return a + b + c;
}
// Add four numbers
public static int add(int a, int b, int c, int d) {
return a + b + c + d;
}
public static void main(String[] args) {
System.out.println("2 + 3 = " + add(2, 3));
System.out.println("1 + 2 + 3 = " + add(1, 2, 3));
System.out.println("1 + 2 + 3 + 4 = " + add(1, 2, 3, 4));
}
}
Output:
2 + 3 = 5
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
🔹 Mixed Parameter Types
Overloading with different combinations of parameter types:
public class PersonInfo {
// Method 1: Name only
public static void createProfile(String name) {
System.out.println("Profile: " + name);
}
// Method 2: Name and age
public static void createProfile(String name, int age) {
System.out.println("Profile: " + name + ", Age: " + age);
}
// Method 3: Name, age, and student status
public static void createProfile(String name, int age, boolean isStudent) {
System.out.println("Profile: " + name + ", Age: " + age + ", Student: " + isStudent);
}
// Method 4: Age and name (different order)
public static void createProfile(int age, String name) {
System.out.println("Profile (age first): " + name + ", Age: " + age);
}
public static void main(String[] args) {
createProfile("Alice");
createProfile("Bob", 25);
createProfile("Charlie", 20, true);
createProfile(30, "Diana");
}
}
Output:
Profile: Alice
Profile: Bob, Age: 25
Profile: Charlie, Age: 20, Student: true
Profile (age first): Diana, Age: 30