Java Reference ยท Object-Oriented Concepts
Method overloading in Java
How Java overload resolution works: the three phases, widening versus boxing versus varargs, null arguments, ambiguity errors and common traps.
Overloading means giving several methods (or constructors) in the same class the same name but different parameter lists. The compiler picks the one to call by looking at the number and the static types of the arguments at each call site. Return type and parameter names play no role.
Because the choice is made at compile time from the declared types, overloading is different from overriding, which is resolved at run time from the actual object.
static void show(int n) { ... } static void show(double d) { ... } static void show(String s) { ... }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Resolution phases
The compiler tries three phases and stops at the first one that finds any applicable method:
- Strict: identity and widening conversions only (no boxing, no varargs).
- Loose: adds boxing and unboxing.
- Varargs: allows variable-arity calls.
Within a phase, the most specific applicable method wins: one whose parameter types can all be passed to the others. If neither of two candidates is more specific, the call is ambiguous and does not compile.
| Call | Candidates | Result |
|---|---|---|
show(5) with int, long, Integer |
all apply | int (exact, phase 1) |
show(5) with only long and Integer |
long by widening (phase 1), Integer by boxing (phase 2) |
long |
f(null) with f(String) and f(Integer) |
both apply, neither more specific | compile error |
f(1, 2) with f(int, long) and f(long, int) |
both apply, neither more specific | compile error |
Usage notes
- Widening beats boxing, boxing beats varargs. That ordering is why
show(5)never picksIntegerwhenlongis available. nullpicks the most specific reference type, and is ambiguous if two unrelated types compete.- Generics and erasure:
void f(List<String>)andvoid f(List<Integer>)clash because both erase toList. - Lambdas make overloads fragile. Two overloads taking different functional interfaces with the same shape can make
f(x -> ...)ambiguous; give the methods different names (asStream.mapToIntdoes). - Prefer distinct names when overloads do different things. Overloads work best for variations of the same operation (
String.valueOf,println). - Constructors can be overloaded too, and
this(...)chains one to another. - Changing the return type alone is not overloading, and the compiler reports a duplicate method.
Related
- Overriding, Polymorphism, Constructors
- Lessons: Method overloading, Polymorphism