How do I overload a method in Java?

Method overloading lets you define multiple methods with the same name but different parameter lists. You can use it to perform a similar task with different inputs.

In this example, you will create three add() methods to add two integers, three integers, or two floating-point numbers.

Source code

public class MethodOverloadingExample {
    void main() {
        int twoIntegerSum = add(10, 20);
        int threeIntegerSum = add(10, 20, 30);
        double twoDoubleSum = add(2.5, 3.75);

        IO.println("Sum of two integers: " + twoIntegerSum);
        IO.println("Sum of three integers: " + threeIntegerSum);
        IO.println("Sum of two doubles: " + twoDoubleSum);
    }

    int add(int first, int second) {
        return first + second;
    }

    int add(int first, int second, int third) {
        return first + second + third;
    }

    double add(double first, double second) {
        return first + second;
    }
}

Explanation

All three methods are named add(), but their parameter lists differ:

Method Parameters
add(int first, int second) Two integers
add(int first, int second, int third) Three integers
add(double first, double second) Two doubles

When you call an overloaded method, the compiler selects the applicable version based on the number and types of the arguments.

The following call passes two int values:

int twoIntegerSum = add(10, 20);

It selects add(int, int) and returns 30.

This call passes three int values:

int threeIntegerSum = add(10, 20, 30);

It selects add(int, int, int) and returns 60.

Finally, this call passes two double values:

double twoDoubleSum = add(2.5, 3.75);

It selects add(double, double) and returns 6.25.

Run the example

Compile and run the file:

javac MethodOverloadingExample.java
java MethodOverloadingExample

Output

Sum of two integers: 30
Sum of three integers: 60
Sum of two doubles: 6.25

Rules to remember

Overloaded methods must differ in the number, types, or order of their parameter types.

Changing only the parameter names does not create a different overload. Changing only the return type is also insufficient. For example, these two declarations cannot appear together in the same class:

int add(int first, int second) {
    return first + second;
}

double add(int first, int second) {
    return first + second;
}

Both methods have the same name and parameter types, so the compiler reports an error.

How do I overload methods in Java?

Method overloading allows a method to use the same name or identifier as the method name as long as the argument list is different. Java can differentiate each method by their method signatures. For example to print some value you can create a print method that accept different kind of objects or values as its parameters.

Overloaded method is differentiated by the number and the type of argument they accept. The print(String string) and print(int number) are distinct and unique due to their argument type.

The compiler does not count a return type as a method differentiator. So it is not legal to create a method with the same name, the same number, the same type of argument but with a different return type.

package org.kodejava.basic;

public class OverloadedExample {
    public void print(Object object) {
        System.out.println("object = " + object);
    }

    public void print(String string) {
        System.out.println("string = " + string);
    }

    public void print(int number) {
        System.out.println("number = " + number);
    }

    public void print(float number) {
        System.out.println("number = " + number);
    }

    public void print(double number) {
        System.out.println("number = " + number);
    }
}