Skip to content

Java OOP

0/36 lessons · 0% complete

— XP

Java OOP · Module 9

Overloading a method

Same name, different parameter lists — chosen at compile time

20m50 XP
→→→

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
1

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.

Four overloads of add

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.

Output
Press Run to execute this example.

Order of parameters counts as a difference

Main.java
1class Printer {
2 void print(String label, int times) {
3 System.out.println(label + " x " + times);
4 }
5
6 void print(int times, String label) {
7 System.out.println(times + " times " + label);
8 }
9}
void print(String label, int times) {
Signature: print(String, int).
void print(int times, String label) {
Signature: 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`).

2

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

  1. Exact match on the argument types wins. add(2, 3) → add(int, int).
  2. Otherwise Java tries widening: a smaller primitive silently becomes a larger one (int → long → float → double). With only add(double, double) available, add(2, 3) still works.
  3. Then boxing (int → Integer), then varargs (add(int...)) — in that order, so a varargs method is always the last resort.
  4. If two candidates are equally good, the compiler refuses to guess: *reference to add is ambiguous*.

Which overload runs?

CallChosen overloadWhy
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
Argument types decide, at compile time

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.

Output
Press Run to execute this example.

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

Wrongint total(int a) { } / long total(int a) { }
WhyIdentical signatures — the return type is not part of the signature.
FixDiffer in parameters, or give the methods different names.
Wrongvoid log(String msg) { } / void log(String text) { }
WhyParameter *names* are irrelevant; both are `log(String)`.
FixChange the types or the number of parameters.
WrongCreating ten overloads that do subtly different things.
WhyOverloads should be the *same* operation with different inputs; otherwise callers cannot predict behaviour.
FixKeep one real implementation and let the short overloads delegate to it.

Lesson playground

Free practice

Four overloads of greet(), one of which delegates in a loop. Add `greet(String name, String title, int times)` reusing the existing methods.

Output
Press Run to execute this example.

Check understanding

Quiz

Question 1 of 5

Three overloads, three calls. What is printed?

Main.java
1class Report {
2 void show(int value) {
3 System.out.println("int version: " + value);
4 }
5
6 void show(double value) {
7 System.out.println("double version: " + value);
8 }
9
10 void show(String value) {
11 System.out.println("String version: " + value);
12 }
13}
14
15public class Main {
16 public static void main(String[] args) {
17 Report report = new Report();
18 report.show(7);
19 report.show(7.5);
20 report.show("7");
21 }
22}

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.