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.example.fundamental;

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);
    }
}
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply