Java OOP · Module 9
Overloading a method
Same name, different parameter lists — chosen at compile time
What you'll be able to do
- Write several methods that share a name but differ in parameters
- List the three legal ways to differ: number, type, order
- Explain why the return type alone cannot distinguish overloads
- Predict which overload the compiler selects for a given call
Method Overloading
One concept, several input shapes
Adding two ints, two doubles and two Strings are the same *idea*. Naming them `addInt`, `addDouble`, `addString` punishes the caller for your bookkeeping.
Method overloading = two or more methods in the same class share a name but have different parameter lists. Java tells them apart by the *signature*: the method name plus the ordered list of parameter types.
You have used it since your first program: System.out.println accepts an int, a double, a String, a boolean — one name, many versions. That is why you never had to remember printlnInt.
Overloads may legally differ in three ways:
1. Number of parameters — add(int, int) vs add(int, int, int)
2. Type of parameters — add(int, int) vs add(double, double)
3. Order of parameter types — print(String, int) vs print(int, String)
The coffee shop that understands you either way
You can say 'a coffee', 'a large coffee', or 'a large coffee with oat milk'. It is one order — order(...) — with three levels of detail, and the barista figures out which one you meant from what you said. You never have to learn three different words for ordering.
Add a fifth overload `double add(double a, int b)` and call it with `calc.add(2.5, 3)`. Notice you never invent a new name.
Order of parameters counts as a difference
print(String, int).print(int, String) — the *types in order* differ, so this is a legal second overload. Renaming the parameters alone would not be enough; parameter names are invisible to the signature.The return type is not part of the signature
``java
int parse(String s) { ... }
long parse(String s) { ... } // ❌ compile error
`
Both have signature parse(String). Java cannot choose based on what you *do* with the result — consider parse("5"); as a standalone statement: there would be no way to decide. Change the parameters, or use different names (parseInt / parseLong`).
Prove it — coding challenges
Overload Resolution
How the compiler picks an overload
Overload selection happens entirely at compile time, using the *declared* types of your arguments — never the runtime object.
The selection order
- Exact match on the argument types wins.
add(2, 3)→add(int, int). - Otherwise Java tries widening: a smaller primitive silently becomes a larger one (
int→long→float→double). With onlyadd(double, double)available,add(2, 3)still works. - Then boxing (
int→Integer), then varargs (add(int...)) — in that order, so a varargs method is always the last resort. - If two candidates are equally good, the compiler refuses to guess: *reference to add is ambiguous*.
Which overload runs?
| Call | Chosen overload | Why |
|---|---|---|
add(2, 3) | add(int, int) | exact match |
add(2.5, 3.5) | add(double, double) | exact match |
add(2, 3.5) | add(double, double) | the int is widened to double |
add(1, 2, 3) | add(int, int, int) | only candidate with three parameters |
add("Ja", "va") | add(String, String) | only candidate accepting Strings |
Delete the `put(int)` overload and re-run: `put(5)` now widens to `put(double)` and prints `double: 5.0`. The call site never changed.
Overloading + delegation = sensible defaults
The cleanest use of overloading is a set of short entry points that all funnel into one real implementation:
``java
void greet(String name) { greet(name, "Hello"); }
void greet(String name, String salutation) { /* the only real logic */ }
`
Same idea as constructor chaining with this(...)` in Module 3: many doors, one room.
Common mistakes
int total(int a) { } / long total(int a) { }void log(String msg) { } / void log(String text) { }Creating ten overloads that do subtly different things.Lesson playground
Four overloads of greet(), one of which delegates in a loop. Add `greet(String name, String title, int times)` reusing the existing methods.
Check understanding
Quiz
Question 1 of 5
Three overloads, three calls. What is printed?
Summary
- Overloading = same method name, different parameter lists, in the same class.
- Legal differences: number, type, or order of parameter types.
- The return type and the parameter names are not part of the signature.
- Resolution happens at compile time: exact match, then widening, then boxing, then varargs.