English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)

Java Reader/Writer

Java Other Topics

Java Method Overloading

In this article, you will learn about method overloading and how to implement overloading in Java with examples.

In Java, if two or moremethodIf the parameters are different (different numbers of parameters, different types of parameters, or both), they can have the same name. These methods are called overloaded methods, and this feature is called method overloading. For example:

void func() { ... }
void func(int a) { ... }
float func(double a) { ... }
float func(int a, float b) { ... }

In this case, the func() method is overloaded. These methods have the same name but accept different parameters.

Please note that the return types of these methods are different. Overloaded methods can have different return types, or they may not have different return types, but they must accept different parameters.

Why method overloading?

Suppose you must perform the addition operation of a given number, but there can be any number of parameters (for simplicity, it can be said2or3parameters).

To complete the task, you can create two methods sum for two and three parameters.2num(int, int) and sum3num(int, int, int). But, other programmers and you in the future may be confused because both methods have the same behavior but different names.

The better way to complete this task is to overload the method, and call one of the overloaded methods based on the parameters passed. This helps to improve the readability of the program.

How to perform method overloading in Java?

The following are different methods to perform method overloading:

1.By changing the number of parameters to overload

class MethodOverloading {
    private static void display(int a){
        System.out.println("Parameter: " + a);
    }
    private static void display(int a, int b){
        System.out.println("Parameter: " + a + " and " + b);
    }
    public static void main(String[] args) {
        display(1);
        display(1, 4);
    }
}

Output:

parameter: 1
parameter: 1 and 4

2.By changing the parameter data type

class MethodOverloading {
    //This method accepts an int
    private static void display(int a){
        System.out.println("Got integer data.");
    }
    //This method accepts a String object
    private static void display(String a){
        System.out.println("Got a String object.");
    }
    public static void main(String[] args) {
        display(1);
        display("Hello");
    }
}

Output:

Got integer data
Got a String object.

Here, both overloaded methods accept a parameter. However, one accepts an int type parameter, while the other accepts a String object.

Let's look at a real example:

class HelperService {
    private String formatNumber(int value) {
        return String.format("%d", value);
    }
    private String formatNumber(double value) {
        return String.format("%.",3f", value);
    }
    private String formatNumber(String value) {
        return String.format("%.",2f", Double.parseDouble(value));}}
    }
    public static void main(String[] args) {
        HelperService hs = new HelperService();
        System.out.println(hs.formatNumber(500));
        System.out.println(hs.formatNumber(89.9934))
        System.out.println(hs.formatNumber("550"));
    }
}

When running the program, the output is:

500
89.993
550.00

NoteIn Java, you can also overload constructors in a way similar to methods.

Recommended Related: Java Constructor Overloading

Points to Note

  • If two or more methods accept different parameters, they can have the same name in the same class. This feature is called method overloading.

  • Method overloading can be achieved in the following ways:

    • Change the number of parameters.

    • Or change the data type of the parameters.

  • Method overloading cannot be achieved by simply changing the return type of the method.