Java method overloading Tutorial

In this section, we will learn what the method overloading is and how they work in Java.

Note: we’re assuming you’re already familiar with methods in Java.

What is Java Method Overloading?

Using method overloading, in a class we can use the same name for multiple methods!

Let’s say someone asked us to write a program that takes 2 numbers, add them together and return the result.

These two values can be either of type double or integer!

At first we might decide to design the class in this way:

public class Addition {
    public double addDouble(double a , double b){
        return a+b;
    }
    public int addInteger(int a , int b){
        return a+b;
    }
}

Of course, from this class we can simply create an object and for values of type `double` we call the `addDouble()` method and for `integer` values we call the `addInteger()` methods like the example below:

public class Simple {
    public static void main(String[] args) {
        Addition addition = new Addition();
        double resD = addition.addDouble(2.3,4.5);
        int resI = addition.addInteger(3, 5);
        System.out.println(resD);
        System.out.println(resI);
    }
}

Output:

6.8

8

Here we called the `addition.addDouble(2.3, 4.5);` and stored the result in the `resD` variable, which is of type `double`.

Also, we called the `addition.addInteger(3 , 5);` and stored the result in the `resI` variable, which is of type `int`.

Next, we sent the values of the `resD` and `resI` to the output stream.

Things are working correctly, and there’s no problem.

But isn’t it more cleanly if we used only one name for both `addDouble` and `addInteger` methods?

For example, both methods are named `add`!

This is exactly what method overloading is about!

How to perform method overloading in Java?

Overloading a method means the name of a method is used multiple times in a class for different methods but they have different structure so that the execution engine can differentiate between them based on the values we put on the methods when they are being called.

Now, in the sections below, we will learn the ways by which we can overload a method in a class.

1- Method overloading via number of parameters

The first way of overloading a method is by using a different number of parameters for the methods with the same names!

For example, if the method A has 3 parameters, then if there’s another method called A in the same class, the other one should have a different number of parameters or nothing at all.

This way when calling such a method, the execution engine can see the number of arguments being passed to the method and so it knows specifically which method to invoke.

Example: overloading via number of parameters

public class Addition {
    public double add(double a , double b, double c){
        return a+b;
    }
    public int add(double a , double b){
        return a+b;
    }
}

Note that here the first `add` method has 3 parameters but the second one has only 2. So now the execution engine knows the first method should only be called when 3 arguments are passed to the `add` method, for example.

2- Method overloading via data type of parameters

The second way of overloading a method is by using different data type for parameters of the target method in the overloaded versions. This way, the execution engine can differentiate between methods just by looking at the data type of arguments we pass to the target method.

For example, if the overloaded method is called A, then the first method should have parameters of data types like double which takes floating values and the others take other data types like `int` or `string` etc! Basically anything other than floating values because the first version of the method took such data type.

Note: if there are multiple parameters for each overloaded version of the method, just make sure the pattern of the data types is not the same for them.

Example: overloading via data type of parameters

public class Addition {
    public double add(double a , double b){
        return a+b;
    }
    public int add(int a , int b){
        return a+b;
    }
}

In this example, the first `add` method has two parameters, but of type double. But the other `add` method has two parameters of type int. For this reason, the compiler can easily differentiate between these two methods.

FAQ:

Can we overload methods on return type?

No! Because execution engines won’t look into the return type of methods to differentiate between overloaded methods.

Can we overload methods that differ only by static keyword?

No!

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies