Method Overloading
Multiple methods with same name
Interview Relevant: Overloading vs Overriding questions
6 min read
Method Overloading in Java
Method overloading allows multiple methods with the same name but different parameters in the same class. It's compile-time polymorphism.
Overloading Rules
- Different parameter count: add(int) vs add(int, int)
- Different parameter types: print(int) vs print(String)
- Different parameter order: display(int, String) vs display(String, int)
⚠️ Cannot Overload By: Return type alone or access modifiers. The parameter list MUST be different!
🔑 Interview Tip: Overloading = Compile-time (static) polymorphism. Overriding = Runtime (dynamic) polymorphism.
💡 Type Promotion: If exact match not found, Java promotes smaller types (byte→short→int→long→float→double).
Code Examples
Overloading by different number of parameters
java
1public class Calculator {
2 // Overloading by number of parameters
3 public int add(int a) {
4 return a;
5 }
6
7 public int add(int a, int b) {
8 return a + b;
9 }
10
11 public int add(int a, int b, int c) {
12 return a + b + c;
13 }
14}
15
16Calculator calc = new Calculator();
17System.out.println(calc.add(5)); // Output: 5
18System.out.println(calc.add(5, 3)); // Output: 8
19System.out.println(calc.add(5, 3, 2)); // Output: 10Overloading by different parameter types
java
1public class Printer {
2 // Overloading by different parameter types
3 public void print(int value) {
4 System.out.println("Integer: " + value);
5 }
6
7 public void print(String value) {
8 System.out.println("String: " + value);
9 }
10
11 public void print(double value) {
12 System.out.println("Double: " + value);
13 }
14
15 public void print(int[] values) {
16 System.out.println("Array: " + Arrays.toString(values));
17 }
18}
19
20Printer p = new Printer();
21p.print(42); // Integer: 42
22p.print("Hello"); // String: Hello
23p.print(3.14); // Double: 3.14
24p.print(new int[]{1,2,3}); // Array: [1, 2, 3]Type promotion and ambiguity issues
java
1// Type Promotion in Overloading
2public class TypePromotion {
3 public void show(int a) {
4 System.out.println("int: " + a);
5 }
6
7 public void show(double a) {
8 System.out.println("double: " + a);
9 }
10}
11
12TypePromotion tp = new TypePromotion();
13tp.show(10); // int: 10 (exact match)
14tp.show(10.5); // double: 10.5 (exact match)
15tp.show('A'); // int: 65 (char promoted to int!)
16tp.show(10L); // double: 10.0 (long promoted to double)
17
18// Ambiguity Example - WON'T COMPILE
19// public void ambiguous(int a, long b) {}
20// public void ambiguous(long a, int b) {}
21// ambiguous(5, 5); // ERROR: Ambiguous!Real-world overloading examples
java
1// Real-world: StringBuilder append() overloading
2StringBuilder sb = new StringBuilder();
3sb.append("Hello"); // append(String)
4sb.append(42); // append(int)
5sb.append(true); // append(boolean)
6sb.append('!'); // append(char)
7sb.append(3.14); // append(double)
8sb.append(new int[]{1}); // append(Object)
9
10// Real-world: Constructor overloading
11public class User {
12 private String name;
13 private int age;
14
15 public User() { // No-arg constructor
16 this("Unknown", 0);
17 }
18
19 public User(String name) { // One-arg constructor
20 this(name, 0);
21 }
22
23 public User(String name, int age) { // All-args constructor
24 this.name = name;
25 this.age = age;
26 }
27}Use Cases
- Constructor overloading for flexible object creation
- Utility methods like println(), append()
- Mathematical operations with different types
- Factory methods with different parameters
- Builder pattern methods
- API design for developer convenience
Common Mistakes to Avoid
- Trying to overload by return type only
- Creating ambiguous overloaded methods
- Not understanding type promotion order
- Confusing overloading with overriding
- Overloading with varargs causing ambiguity
- Using auto-boxing ambiguity (int vs Integer)