Java method overloading

In this example we cab see how use method overloading in Java, multiple methods can have the same name in the same class with different parameters. This is possible because methods in Java are distinguished by their method signature. The method signature is the combination of the method name and the parameter list. In Java return type is not part of the method signature.

Example with overloading methods:

import java.util.Arrays;
import java.util.List;

public class MainClassJavaMethodOverloading {
    public static void main(String[] args) {

        // here it will find the method that accept single Integer as argument
        myMethod(10);

        // here it will find the method that accept single double as argument
        // 10d -> d stays for double
        myMethod(10d);

        // Here it will not find method with String argument that why
        // it will go to method that accept parent class as parameter
        // In this case the method with Object parameter
        myMethod("String Test");

        // Here the method with the two parameters will be called and not
        // the method with Varargs (Integer...)
        // The method with exact amount of parameters have higher priority
        // when Java have to decide which to call
        myMethod(10, 20);

        // Here the method with the three Object parameters will be called and not
        // the method with Varargs (Integer...)
        // The method with exact amount of parameters have higher priority event if
        // accept Parent Class as argument
        myMethod(10, 20, 30);

        // here the method with Varargs (Integer...) params will be called
        myMethod(10, 20, 30, 40);

        // here the method with Varargs (Integer...) param will be called
        // because we do not have method without parameters
        myMethod();
    }

    public static void myMethod(Integer i)
    {
        System.out.println("Called method with one Integer parameter: " + i);
    }

    public static void myMethod(Double d)
    {
        System.out.println("Called method with one Double parameter: " + d);
    }

    public static void myMethod(Object o)
    {
        System.out.println("Called method with one Object parameter: " + o);
    }

    public static void myMethod(Integer a, Integer b)
    {
        System.out.printf("Called method with TWO Integer parameters: %%d, %%d ", a, b);
        System.out.println();
    }

    public static void myMethod(Object a, Object b, Object c)
    {
        System.out.printf("Called method with Three Objects parameters: %%s, %%s, %%s ", a, b, c);
        System.out.println();
    }

    public static void myMethod(Integer... a)
    {
        List<Integer> l = Arrays.asList(a);
        System.out.println("Called method with Varargs (Integer...) parameters: " + l);
    }
}

Output:

Called method with one Integer parameter: 10
Called method with one Double parameter: 10.0
Called method with one Object parameter: String Test
Called method with TWO Integer parameters: 10, 20 
Called method with Three Objects parameters: 10, 20, 30 
Called method with Varargs (Integer...) parameters: [10, 20, 30, 40]
Called method with Varargs (Integer...) parameters: []

Leave a Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.