Module 4 · Methods — topic 2 of 3
Method Overloading
Defining several methods with the same name and different parameter lists, how Java chooses between them, and using overloading to provide default values.
Learning objectives
- Write overloaded methods
- Predict which overload the compiler chooses
- Use overloading to simulate default arguments
Definition
Method overloading is declaring multiple methods in the same class with the same name but different parameter lists (number, types or order of parameters). The compiler selects the most specific applicable method at compile time.
Introduction
System.out.println works with an int, a double, a String or an object. That isn't magic — PrintStream declares many methods all called println, each taking a different type. This is overloading, and you can use it in your own classes to offer convenient variations of an operation.
Why it matters
Overloading produces natural APIs: area(radius), area(width, height).
Understanding how Java resolves overloads explains surprising calls, such as remove(1) on a List<Integer> removing by index rather than by value.
Detailed explanation
What counts. Overloads must differ in the number, types or order of parameters. The return type and parameter names do not count: two methods that differ only in return type are a compile error.
Resolution happens at compile time. The compiler looks for the most specific method that accepts the arguments, in three phases: first without boxing or varargs (exact types and widening such as int → long → double), then allowing boxing/unboxing (int ↔ Integer), and finally varargs.
Widening beats boxing. Given f(long) and f(Integer), the call f(5) chooses f(long), because widening is tried before boxing.
Ambiguity. If two candidates are equally specific — for example f(int, double) and f(double, int) called as f(1, 2) — the call doesn't compile.
Default values. Java has no default arguments, so a common pattern is to overload a method and have the shorter versions call the longest one with default values.
Overloading is sometimes called *compile-time* or *static* polymorphism, in contrast with *overriding* (module 5), which is resolved at run time.
Syntax
- Same name + different parameter lists = overloads.
- Return type alone cannot distinguish methods.
- Shorter overloads usually delegate to the most general one.
Code examples
Overloads by number and type
12.57 13.5 25
area(5) picks the int version (exact match); area(2.0) picks the double version.
Default values through delegation
Hello, Asha! Welcome, Ravi! Go, Team! Go, Team! Go, Team!
More examples
Which overload is chosen?
int long double int int Integer Object varargs varargs g(long) [30]
Step-by-step explanation
f(5)matchesf(int)exactly;f(5L)matchesf(long); afloatwidens todouble.- A
byteand acharboth widen toint, the closest primitive overload. - An
Integerargument matchesf(Integer)exactly; aStringonly fitsf(Object); zero or two ints need varargs. g(5)choosesg(long): widening (phase 1) is preferred over boxing to Integer (phase 2).- On a
List<Integer>,remove(1)callsremove(int index), removing 20; to remove a value you must pass anIntegerobject, which callsremove(Object).
Expected output
Running “Which overload is chosen?” prints exactly:
Practical applications
println,Math.maxandString.valueOfare heavily overloaded in the JDK- Constructors are often overloaded to allow optional settings
- Builder-like APIs offer shorter overloads with sensible defaults
Best practices
- Overloads should do the same conceptual thing.
- Implement shorter overloads by calling the most complete one.
- Avoid overloads that differ only by a boxed versus primitive type; they confuse readers.
Common mistakes
Overloading by return type only
Avoid
Prefer
The compiler can't tell which one a call means, so methods that differ only in return type are rejected. Give them different names.
Ambiguous overloads
Avoid
Prefer
Both methods need one widening conversion, so neither is more specific and the call fails to compile.
Important notes
- Overload resolution uses the static (declared) types of the arguments, not their run-time types.
- Varargs overloads are chosen last, which is why
f(int...)lost to every single-argument overload above.
Key takeaways
- Overloads share a name but differ in parameter lists.
- Resolution order: exact/widening, then boxing, then varargs.
- Use overloading to provide default values.
Try it yourself
- 1Write overloaded
maxmethods for two ints, three ints and an int array. - 2Write
log(String message)andlog(String message, String level)where the first defaults to "INFO".
Finished reading? Mark it complete to update your dashboard.
Suggested practice
5 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhich pair of methods is NOT a valid overload?
- BeginnerWhat is printed?
- ModerateWhat is printed?