Java Method Overloading

Java Method Overloading

method overriding in java

In this tutorial, you’ll find out about the method overloading and how you can achieve it in Java with the help of examples.

In Java, at least two methods can have the same name if they vary in parameters (distinctive number of parameters, various sorts of parameters, or both). These methods are called over-burden methods and this element is called method over-burdening. For instance:

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 arguments.

Notice that, the return type of these methods isn’t the equivalent. Over-burden strategies could possibly have diverse return types, however they should contrast in parameters they accept.


Why method overloading?

Assume, you need to play out the expansion of given numbers yet there can be quite a few contentions (suppose either 2 or 3 arguments for simplicity).

In order 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, as well as you in the future may get confused as the behavior of both methods are the same but they differ by name.

The better method to achieve this undertaking is by over-burdening methods. Furthermore, contingent on the contention passed, one of the over-burden techniques is called. This helps to increase the readability of the program.


How to perform method overloading in Java?

Here are different ways to perform method overloading:

1. Overloading by changing the number of arguments

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 datatype of parameters

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 argument. However, one accepts the argument of type int whereas other accepts String object.


Let’s look at a real-world 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 you run the program, the output will be:

500
89.993
550.00

Note: In Java, you can also overload constructors in a similar way like methods.


Important Points

Two or more methods can have same name inside the same class if they accept different arguments. This feature is known as method overloading.
Method overloading is achieved by either:
changing the number of arguments.
or changing the datatype of arguments.
Method overloading is not possible by changing the return type of methods.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

Leave a Reply

Your email address will not be published. Required fields are marked *