Skip to content
wannacode

Overloading

Problem

Two items, three items

The till has to add up the prices of the items. Sometimes there are two, sometimes three. The method means the same thing either way: "add up". How do you write it for both cases without making up names like total2 and total3?

Here is a hint: you already use a method that takes different things. System.out.println prints 5, 2.5 and text too. How does it do that?

The idea

One name, different parameters

Two methods can have the same name if their parameters differ: in number, in type or in order. This is called overloading.

static double total(double a, double b) {
return a + b;
}
static double total(double a, double b, double c) {
return a + b + c;
}
System.out.println(total(2.5, 4.0));
System.out.println(total(2.5, 4.0, 1.5));
Output
6.5
8.0

At each call, the compiler looks at the arguments and picks the method that fits. Two arguments: the first method. Three: the second one. That is also why println works with everything: it is many methods with one name.

static double total(double a, double b) {
return a + b;
}
static double total(double a, double b, double c) {
return a + b + c;
}
System.out.println(total(2.5, 4.0));
System.out.println(total(2.5, 4.0, 1.5));
Step by step, in words
  1. A call with two arguments: total(2.5, 4.0).
  2. There are two methods with one name. The compiler counts the arguments: two. The method with two parameters fits.
  3. The arguments go into a and b. The method returns 6.5.
  4. Now there are three arguments.
  5. The other method fits, the one with three parameters. It returns 8.0.
  6. One name, two methods. The arguments decide which one is called.

Try it

Which method?

What will the program print?

static void describe(int x) {
System.out.println("whole: " + x);
}
static void describe(double x) {
System.out.println("decimal: " + x);
}
describe(5);
describe(5.0);
describe(7 / 2);
Answer

Try it

Find the bug

The program does not compile. Which line is the problem on?

Pick the line with the bug.

The idea

As many arguments as you like

What if there are five items? Twenty? You do not write a method for each count. You say that the method takes any number of arguments of one type:

static double total(double... prices) {
double sum = 0;
for (double p : prices) {
sum += p;
}
return sum;
}
System.out.println(total(1.5, 2.0, 3.5, 4.0));
System.out.println(total(9.99));
System.out.println(total());
Output
11.0
9.99
0.0

The three dots after the type (double... prices) mean: "pass as many numbers as you like". Inside the method they arrive as an array, which can also be empty.

More precisely, if you are curious
  • There can be only one such parameter, and it has to be the last one: print(String title, double... prices).
  • If there is a method with exactly two parameters and also a method with any number, a call with two arguments goes to the first one. The exact match wins.

Try it

What is missing?

static double total(double prices) {
    double sum = 0;
    for (double p : prices) {
        sum += p;
    }
    return sum;
}

Exercise

One name, several methods

1. The largest. Write three methods with one name, max: for two whole numbers, for three whole numbers (it calls the first one twice) and for two decimals.

Calls
max(3, 8)
max(4, 9, 2)
max(2.5, 1.5)
Output
8
9
2.5

2. Average grade. The method average(int... grades) returns the average of any number of grades, and with no grades it returns 0.

Calls
average(5, 6, 4)
average(6, 5)
average()
Output
5.0
5.5
0.0

3. A receipt line. printLine(String item, int qty) prints "Coffee x 2". Add printLine(String item) for a single item. It does not repeat the code: it calls the first method with 1.

Output
Coffee x 2
Croissant x 1

Test

Check yourself

1. When can two methods in one class have the same name?
2. How does the compiler choose which of the overloaded methods to call?
3. What is double... prices inside the method?
4. Which of these does NOT compile?

Made by Ivo Gergov as part of Evo Tech's campaign for accessible programming education ·