Method
Overloading
Learn how Java lets you define multiple methods with the same name but different parameters: one of the most powerful tools for writing clean, flexible code.
What is Method Overloading?
In Java, method overloading means defining two or more methods in the same class that share the same name but have different parameter lists. The compiler decides which version to call based on the arguments you pass.
Why Overloading Matters
One Name, Many Signatures
The Rules of Overloading
What Is a Method Signature?
Overloading vs Overriding
| Overloading | Overriding | |
|---|---|---|
| Where | Same class | Parent → Child class |
| Parameters | Must differ | Must be identical |
| Return type | Can differ | Same or covariant |
| Polymorphism | Compile-time | Run-time |
| Annotation | None required | @Override |
How to Apply Overloading
Example 1: Different Number of Parameters
static int add(int a, int b) {
return a + b;
}
static int add(int a, int b, int c) {
return a + b + c;
}
}
Example 2: Different Types
static void display(int value) {
System.out.println("Integer: " + value);
}
static void display(String value) {
System.out.println("String: " + value);
}
static void display(double value) {
System.out.println("Double: " + value);
}
}
Example 3: Different Order of Types
static void show(int a, String b) {
System.out.println(a + " then " + b);
}
static void show(String a, int b) {
System.out.println(a + " then " + b);
}
}
Type Promotion and Overloading
If no exact match exists, Java automatically promotes smaller types to larger ones: byte → short → int → long → float → double.
static void test(double d) {
System.out.println("double: " + d);
}
}
// In main:
PromoDemo.test(5); // int 5 promoted to double 5.0
What Does Not Count?
static int add(int a, int b) { return a + b; }
static double add(int x, int y) { return x + y; }
Real-World Use Case
static void log(String message) {
System.out.println("[INFO] " + message);
}
static void log(String message, int code) {
System.out.println("[ERR " + code + "] " + message);
}
static void log(String message, String level) {
System.out.println("[" + level + "] " + message);
}
}
Common Mistakes
Summary Cheat Sheet
| Criterion | Valid Overload? | Example |
|---|---|---|
| Different number of params | ✓ Yes | add(int) vs add(int, int) |
| Different param types | ✓ Yes | add(int) vs add(double) |
| Different param order | ✓ Yes | f(int, String) vs f(String, int) |
| Different return type only | ✕ No | int add(int) vs double add(int) |
| Different param names only | ✕ No | add(int a) vs add(int x) |
Question 1 of 5
Which of the following is required for two methods to be considered overloaded?
Question 2 of 5
Given these two methods in the same class:
int calc(int a) and double calc(int a)
What happens when you compile?
Question 3 of 5
A class has display(int x) and display(double x). What happens when you call display(5)?
Question 4 of 5
Which pair of methods is a valid example of overloading?
Question 5 of 5
Method overloading is an example of which type of polymorphism?