Method Overloading
Method overloading is a feature in Java that allows a class to have multiple methods with the same name but different parameter lists. It improves code readability, reusability, and flexibility and is a classic example of compile-time polymorphism. This is a very common interview topic.
What Is Method Overloading?
- Same method name
- Different parameter list
- Occurs within the same class (or inherited)
- Resolved at compile time
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
Why Method Overloading Is Used
- Improves readability
- Provides logical grouping of related operations
- Allows same action with different inputs
- Reduces method name explosion
Rules of Method Overloading (Very Important)
1. Parameter List Must Be Different
Overloading can differ by:
- Number of parameters
- Type of parameters
- Order of parameters
add(int a, int b)
add(int a, int b, int c)
add(double a, double b)
add(int a, double b)
2. Return Type Alone Cannot Overload a Method ❌
int test() { }
double test() { } // Compile-time error
3. Access Modifiers Do NOT Matter for Overloading
public void show(int a) { }
private void show(double a) { }
✔ Valid overloading
Types of Method Overloading
1. By Number of Parameters
void display() { }
void display(int a) { }
2. By Data Type of Parameters
void display(int a) { }
void display(String a) { }
3. By Order of Parameters
void display(int a, String b) { }
void display(String b, int a) { }
Method Overloading with Type Promotion
Java performs automatic type promotion.
void show(int a) {
System.out.println("int");
}
void show(double a) {
System.out.println("double");
}
show(10); // calls show(int)
show(10.5); // calls show(double)
Ambiguity in Method Overloading
void test(int a, double b) { }
void test(double a, int b) { }
test(10, 10); // Compile-time error (ambiguous)
Method Overloading with String
void print(String s) {
System.out.println("String");
}
void print(Object o) {
System.out.println("Object");
}
print("Java"); // calls String version
Method Overloading with null
void show(String s) {
System.out.println("String");
}
void show(Object o) {
System.out.println("Object");
}
show(null); // calls String (more specific)
Overloading main() Method
Yes, main() can be overloaded.
public static void main(String[] args) {
System.out.println("Main method");
}
public static void main(int a) {
System.out.println("Overloaded main");
}
⚠️ JVM always calls:
public static void main(String[] args)
Compile-Time Polymorphism Explained
- Method call resolution happens at compile time
- Based on reference type and arguments
- Faster than runtime polymorphism
Common Beginner Mistakes
- Changing only return type
- Confusing overloading with overriding
- Creating ambiguous overloads
- Misunderstanding type promotion
- Overusing overloading unnecessarily
Interview-Ready Answers
Short Answer
Method overloading allows multiple methods with the same name but different parameter lists.
Detailed Answer
In Java, method overloading is a compile-time polymorphism feature where methods share the same name but differ in number, type, or order of parameters. The return type alone cannot differentiate overloaded methods.
Key Takeaway
Method overloading provides flexibility and cleaner APIs, but it must be used carefully to avoid ambiguity and maintain readability.