Beginner friendly series – Understanding Java Method Overloading

Hits: 0

Article directory

In this article, you will learn about method overloading and how to implement overloading in Java with the help of examples.
    In Java, you can use the same name if two or more methods have different parameters (different number of parameters, different parameter types, or both). These methods are called overloaded methods, and this feature is called method overloading. E.g:

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

Here, the func() method is overloaded. These methods have the same name but accept different parameters.
    Note that these methods have different return types. Overloaded methods may or may not have different return types, but the parameters they accept must be different.

Why do you need method overloading?

Suppose you have to perform an addition of a given number, but you can have any number of arguments (2 or 3 for simplicity).
    To accomplish the task, you can create two methods sum2num(int, int) and sum3num(int, int, int) for two and three parameters respectively. However, other programmers and you may be confused in the future because the two methods behave the same but have different names.
    A better way to accomplish this task is to overload methods. And, depending on the parameters passed, one of the overloaded methods is called. This helps improve the readability of the program.

How to perform method overloading in Java?

Here are the different ways to perform method overloading:
1. Overload by changing the number of parameters

class MethodOverloading {
    private static void display(int a){
        System.out.println("Arguments: " + a);
    }

    private static void display(int a, int b){
        System.out.println("Arguments: " + a + " and " + b);
    }

    public static void main(String[] args) {
        display(1);
        display(1, 4);
    }
}

output:

Arguments: 1
Arguments: 1 and 4

2. By changing the data type of the parameter

class MethodOverloading {

    // this method accepts int
    private static void display(int a){
        System.out.println("Got Integer data.");
    }

    // this method  accepts String object
    private static void display(String a){
        System.out.println("Got String object.");
    }

    public static void main(String[] args) {
        display(1);
        display("Hello");
    }
}

output:

Got Integer data.
Got String object.

Here, both overloaded methods accept one parameter. However, one accepts a parameter of type int, while the other accepts a String object.
    Let’s see 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:

89.993
550.00

Note: In Java, you can also overload constructors in a method-like fashion.

Important

  • 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 implemented in either of the following ways:
    • Change the number of parameters
    • or change the data type of the parameter
  • A method cannot be overloaded by changing its return type.

Reference documentation

[1]Parewa Labs Pvt. Ltd.Java Generics[EB/OL].https://www.programiz.com/java-programming/method-overloading,2020-01-01.

You may also like...

Leave a Reply

Your email address will not be published.