JavaBasic syntax and simple programsMethods

Overloading

9 seconds read

Methods overloading

If methods have the same name, but a different number or type of parameters, they are overloaded. It means you can invoke different methods by the same name by passing different arguments.

As an example, let's consider some overloaded method from the standard class Math:

public static int abs(int a) { return (a < 0) ? -a : a; }

public static float abs(float a) { return (a <= 0.0F) ? 0.0F - a : a; }

These methods have the same name but different type of the argument. They are overloaded.

Important note: it's impossible to declare more than one method with the same name and parameters (number and types), even with different return types. The return type is not considered for overloading because it's not a part of the signature.

Here are three methods print for printing different values.

public static void print(String stringToPrint) {
    System.out.println(stringToPrint);
}

public static void print(String stringToPrint, int times) {
    for (int i = 0; i < times; i++) {
        System.out.println(stringToPrint);
    }
}

public static void print(int val) {
    System.out.println(val);
}

The first method prints an input string, the second one prints an input string a given number of times and the last one prints an integer value. These methods are overloaded.

Let's invoke these methods:

print("some string");
print("another string", 2);
print(5);

As you can see, it's possible to call any of these methods by the same name passing suitable arguments. The code outputs:

some string
another string
another string
5

The overloading mechanism allows us not to write different names for methods that perform the similar operations.

Looking ahead, we note that overloading is a form of the static (compile-time) polymorphism.

Overloading and casting

In the case, where the type of a method parameter is not exactly the same as the type of the passed argument, the compiler chooses the method that has the closest type of the argument in order of the implicit casting.

Let's see an example.

public class OverloadingExample {

    public static void print(short a) {
        System.out.println("short arg: " + a);
    }

    public static void print(int a) {
        System.out.println("int arg: " + a);
    }

    public static void print(long a) {
        System.out.println("long arg: " + a);
    }

    public static void print(double a) {
        System.out.println("double arg: " + a);
    }

    public static void main(String[] args) {
        print(100);
    }
}

Let's call print(100), the program outputs:

int arg: 100

Let's remove or comment the method public static void print(int a), then recompile and run the program again.

The result is:

long arg: 100

Ok, now, let's remove the method public static void print(long a) too. After recompiling the program outputs:

double arg: 100.0

If we remove the method public static void print(double a) the program can't be compiled.

In this way, the compiler chooses the most suitable method in the order of implicit casting. If it's impossible we must cast an argument explicitly like in the following example:

 public class OverloadingExample {

    public static void print(short a) {
        System.out.println("short arg: " + a);
    }

    public static void main(String[] args) {
        print((short) 100);  // explicit casting 
    }
}
3 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo