Skip to main content

18. Overloading

 

Overloading

Method overloading is a feature that allows a class to have multiple methods with the same name but different parameter lists within the same class.

  • Same Method Name: All methods must have the same name.
  • Different Parameter Lists: The methods must differ in their parameter lists. This can involve differences in the number of parameters or differences in the types of parameters.
  • Return Type: The return type is not considered when overloading a method. Two methods with the same name and parameter lists but different return types are not considered overloaded; they would result in a compilation error.

When a class has overloaded methods, one of the compiler's jobs is to determine which method to use whenever it finds an invocation for the overloaded methods.

In every case, when an exact match isn't found, the JVM uses the method with the smallest argument that is wider than the parameter.

class ShellClass {
    public static void main(String[] args) {
        byte b = 5;
        short s = 5;
        long l = 5;
        float f = 5.0f;

        go(b);
        go(s);
        go(l);
        go(f);
    }

    static void go(int x) {
        System.out.println("int");
    }

    static void go(long x) {
        System.out.println("long");
    }

    static void go(double x) {
        System.out.println("double");
    }
}

Overloading with Boxing, Widening and Var-args

  • The compiler will choose widening over boxing.
  • class ShellClass {
        public static void main(String[] args) {
            int i = 5;
            go(i);
        }

        static void go(Integer x) {
            System.out.println("Integer");
        }

        static void go(long x) {
            System.out.println("long");
        }
    }

  • The compiler will choose widening over var-args.
  • class ShellClass {
        public static void main(String[] args) {
            int i = 5;
            go(i, i);
        }

        static void go(long x, long y) {
            System.out.println("Long, Long");
        }

        static void go(long... x) {
            System.out.println("long...");
        }
    }

  • The compiler will choose boxing over var-args.
  • class ShellClass {
        public static void main(String[] args) {
            int i = 5;
            go(i, i);
        }

        static void go(Integer x, Integer y) {
            System.out.println("Integer, Integer");
        }

        static void go(int... x) {
            System.out.println("int...");
        }
    }

  • Compiler can't widen from one wrapper type to another.
  • class ShellClass {
        public static void main(String[] args) {
            byte b = 5;
            go(b);
        }

        static void go(Long x) {
            System.out.println("Long");
        }
    }

  • However, compiler can perform a boxing operation followed by a widening operation in order to match an invocation to a method.
  • class ShellClass {
        public static void main(String[] args) {
            byte b = 5;
            go(b);
        }

        static void go(Object x) {
            System.out.println("Object");
        }
    }

  • We can combine var-args with either widening or boxing, but sometimes its can produce ambiguity.
  • class ShellClass {
        public static void main(String[] args) {
            int i = 5;
            go(i);
        }

        static void go(long... x) {
            System.out.println("long...");
        }

        static void go(Integer... x) {
            System.out.println("Integer...");
        }
    }


Prev Next

Comments