Method Overloading JAVA

Method Overloading JAVA

Java being an object-oriented language means everything in Java is a class. Therefore if you are transitioning from a function-based language I would like you to know that functions are just the same as methods, just that they are defined inside a class and bound to that specific class.

Now let's talk about "method overloading".

What is method overloading?

Method overloading means defining multiple methods with the same name but with different signatures.

This is a situation where multiple methods of the same name are created but with different return parameters, or a different number of parameters. Let's look at the examples below for clarity

Fig A

class MethodOverloading{
    public static int max(int a, int b){
        System.out.println("Max with two int parameters in action")
        // return the maximum value between a and b
    }
    public static double max(double a, double b){
        System.out.println("Max with two double parameters in action")
        // return the maximum value between a and b
    }
    public static String max(String a, String b){
        System.out.println("Max with two String parameters in action")
        // return the maximum value between a and b
    }
}

Fig B

class MethodOverloading{
    public static int max(int a, int b){
        System.out.println("Max with two int parameters in action")
        // return the maximum value between a and b
    }
    public static int max(int a, int b, int c){
        System.out.println("Max with three int parameters in action")
        // return the maximum value between a and b
    }
}

In Fig A. It was observed that each method had different types of parameters, but all the methods had the same name. You might expect this code to give an error when compiled, but no it won't. The reason is that the compiler sees each method differently based on the fact that they each have different parameter types, one is an int, the other is a double type and another is a String type.

The logic behind method overloading is having the same method name for a specified action in different scenarios. If the max method were called with an int parameter, the first method is called, if the max method were to be called with a double parameter, the second method is called and the same goes for string with the third method. You will see that in action in the next part.

In Fig B, Here we have another form of overloading which has to do with the number of parameters. The first max method here has two parameters while the second has three parameters. When a call is made to max with two int parameters given, the first method is called, but with three parameters the second method is called

Let's take a look at method overloading in action.

class TestMethodOverloading {
    public static void main(String[] args){
        MethodOverloading method = new MethodOverloading();
        method.max(2,3); // returns"Max with two int parameters in         action"
        method.max(2.0,3.0); // returns"Max with two double parameters in action"
        method.max("james","john"); // returns"Max with two String parameters in action"
        method.max(2,3,4); // returns" Max with three int parameters in action"
    }
}

On calling the max method the compiler or interpreter matches the provided method parameters to the accurate methods.

On a final note, method overloading is useful when you have different use cases for a method, instead of giving different method names, you use a single method name to define all implementations. You get to see more of it's importance when creating the constructors of classes with different implementations.

That's all for now on method overloading.

Let me know your questions and suggestions in the comment section. Thanks for reading.