Method Overloading

Method Overloading #

In Java it is possible to have multiple methods with same name as long as parameter signatures are different, ie either methods have different parameter types or there are different number of parameters. This is called method overloading.

As an example the following class has three diferent parameters with same names.

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

    public long plus(long a, long b) {
        return a + b;
    }

    public int plus(int a, int b, int c) {
        return a + b + c;
    }
}

In the above example the first method will work only when method plus is invoked with two integers, the second method will run only when plus is invoked with longs, and the third plus will run only when it is invoked with three integers.