DEV Community

Cover image for Overloading Methods
Paul Ngugi
Paul Ngugi

Posted on

Overloading Methods

Overloading methods enables you to define the methods with the same name as long as their signatures are different.

The max method that was used earlier works only with the int data type. But what if you need to determine which of two floating-point numbers has the maximum value? The solution is to create another method with the same name but different parameters, as shown in the following code:

public static double max(double num1, double num2) {
if (num1 > num2)
return num1;
else
return num2;
}
Enter fullscreen mode Exit fullscreen mode

If you call max with int parameters, the max method that expects int parameters will be invoked; if you call max with double parameters, the max method that expects double parameters will be invoked. This is referred to as method overloading; that is, two methods have the same name but different parameter lists within one class. The Java compiler determines which method to use based on the method signature.

Below is the program that creates three methods. The first finds the maximum integer, the second finds the maximum double, and the third finds the maximum among three double values. All three methods are named max.

package demo;

public class TestMethodOverloading {

    public static void main(String[] args) {
        // Invoke the max method with int parameters
        System.out.println("The maximum of 3 and 4 is " + max(3, 4));

        // Invoke the max method with the double parameters
        System.out.println("The maximum of 3.0 and 5.4 is " + max(3.0, 5.4));

        //Invoke the max method with three double parameters
        System.out.println("The maximum of 3.0, 5.4, and 10.14 is " + max(3.0, 5.4, 10.14));
    }

    /** Return the max of two int values */
    public static int max(int num1, int num2) {
        if(num1 > num2)
            return num1;
        else
            return num2;
    }

    /** Find the max of two double values */
    public static double max(double num1, double num2) {
        if(num1 > num2)
            return num1;
        else
            return num2;
    }

    /** Return the max of three double values */
    public static double max(double num1, double num2, double num3) {
        return max(max(num1, num2), num3);
    }

}

Enter fullscreen mode Exit fullscreen mode

When calling max(3, 4) (line 7), the max method for finding the maximum of two integers is invoked. When calling max(3.0, 5.4) (line 10), the max method for finding the maximum of two doubles is invoked. When calling max(3.0, 5.4, 10.14) (line 13), the max method for finding the maximum of three double values is invoked.

Can you invoke the max method with an int value and a double value, such as max(2, 2.5)? If so, which of the max methods is invoked? The answer to the first question is yes. The answer to the second question is that the max method for finding the maximum of two double values is invoked. The argument value 2 is automatically converted into a double value and passed to this method.

You may be wondering why the method max(double, double) is not invoked for the call max(3, 4). Both max(double, double) and max(int, int) are possible matches for max(3, 4). The Java compiler finds the method that best matches a method invocation. Since the method max(int, int) is a better matches for max(3, 4) than max(double, double), max(int, int) is used to invoke max(3, 4).

Overloading methods can make programs clearer and more readable. Methods that perform the same function with different types of parameters should be given the same name. Overloaded methods must have different parameter lists. You cannot overload methods based on different modifiers or return types.

Sometimes there are two or more possible matches for the invocation of a method, but the compiler cannot determine the best match. This is referred to as ambiguous invocation. Ambiguous invocation causes a compile error. Consider the following code:

public class AmbiguousOverloading {
 public static void main(String[] args) {
 System.out.println(max(1, 2));
 }
public static double max(int num1, double num2) {
if (num1 > num2)
return num1;
else
return num2;
 }
 public static double max(double num1, int num2) {
if (num1 > num2)
return num1;
else
return num2;
 }
}
Enter fullscreen mode Exit fullscreen mode

Both max(int, double) and max(double, int) are possible candidates to match max(1, 2). Because neither is better than the other, the invocation is ambiguous, resulting in a compile error.

Top comments (0)