Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Method overloading

Overloading means several methods with the same name but different parameters. It lets you give one name to actions that do the same thing with different data. You have already used overloading: IO.println() accepts strings, numbers and boolean values.

Different number of parameters

Methods with the same name can differ in the number of parameters. Java picks the version by the number of arguments in the call:

int sum(int a, int b) {
    return a + b;
}
 
int sum(int a, int b, int c) {
    return a + b + c;
}
 
void main() {
    IO.println(sum(2, 3));
    IO.println(sum(2, 3, 4));
}

Output:

5
9

Different parameter types

Versions can also differ in parameter types. Then Java looks at the types of the arguments:

void describe(int value) {
    IO.println("Whole number: " + value);
}
 
void describe(double value) {
    IO.println("Fractional number: " + value);
}
 
void describe(String value) {
    IO.println("String: " + value);
}
 
void main() {
    describe(10);
    describe(2.5);
    describe("Java");
}

Output:

Whole number: 10
Fractional number: 2.5
String: Java

A method's name together with its list of parameter types is called its signature. Overloaded methods must have different signatures. Parameter names are not part of it: describe(int value) and describe(int number) are the same method.

How Java picks a version

First Java looks for an exact type match. If there is none, it tries automatic widening, for example int → long → double:

void show(long value) {
    IO.println("long: " + value);
}
 
void show(double value) {
    IO.println("double: " + value);
}
 
void main() {
    show(5);
    show(5.0f);
}

Output:

long: 5
double: 5.0

There is no method for int, so 5 goes to the nearest wider type — long. A float widens to double.

Default values

Java has no default parameter values. Overloading replaces them: the short version calls the full one and fills in the missing argument:

void order(String drink, int cups) {
    IO.println(drink + " x" + cups);
}
 
void order(String drink) {
    order(drink, 1);
}
 
void main() {
    order("Latte", 3);
    order("Espresso");
}

Output:

Latte x3
Espresso x1

The logic lives in one method, and the other versions only pass data to it.

When overloading fits

Overload methods that do the same thing. If the actions differ, give them different names: findById and findByName are clearer than two find methods with similar parameters.

Common mistake

Methods cannot be overloaded by return type alone — for the call total(2, 3) Java could not choose a version:

int total(int a, int b) {
    return a + b;
}
 
double total(int a, int b) {
    return a + b;
}
 
void main() {
    IO.println(total(2, 3));
}

Output:

Main.java:5: error: method total(int,int) is already defined in class Main
double total(int a, int b) {
       ^
1 error
error: compilation failed

The parameter lists must differ in the number, types or order of parameters.

Thanks for reading!

Donate to the site — we work hard for you.