-1

Can anyone explain the output of the below program when null is passed as a parameter

 public class TestThis {

        public void method(Object o){
            System.out.println("object method");
        }

    public void method(int[] intArray){
        System.out.println("int array method");
    }

    public static void main(String[] args) {
        new TestThis().method(null);
    }

}

Output:- int array method

public class TestThis {

    public void method(Object o){
        System.out.println("object method");
    }

    public void method(int[] intArray){
        System.out.println("int array method");
    }

    public void method(double[] doubleArray){
        System.out.println("double array method");
    }

    public static void main(String[] args) {
        new TestThis().method(null);
    }

}

Compile time error:- The method method(Object) is ambiguous for the type TestThis

yeppe
  • 679
  • 1
  • 11
  • 43

2 Answers2

1

The compiler sees that you have two methods that could both take Object as parameter (because an array of double is an Object, too).

So, when you only pass null, the compiler can't tell which method you want to call.

What would work instead:

new TestThis().method((double[]) null);

For the record: the thing is - this is not about polymorphism, it is about overloading. The compiler has to fix which of your overloaded methods to invoke at compile time. Therefore the complains to you, if your source code doesn't give enough information to make that decision.

And to be precise: in your first example, there are two methods, and the compiler picks the one for (Object).

But in your second example, you got three methods, and two are more specific than (Object). That is why it refuses to compile example 2.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Wouldnt an int array be an object aswell? – M. Reyes Sep 23 '16 at 08:28
  • @yeppe: No, `null` is of the [null type](https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.1). – T.J. Crowder Sep 23 '16 at 08:28
  • Edited my question. Went down the the wrong path for that aspect of the question. – GhostCat Sep 23 '16 at 08:29
  • "two are more specific than (Object)" There's a bit missing here: you can have two methods more specific than `Object` without ambiguous overload, e.g. if you have overloads for `Object`, `CharSequence` and `String`. Could you make this more precise? – Andy Turner Sep 23 '16 at 08:35
1

The method having most specific / least generic type is chosen. Object is the superclass of all other classes, so, in the first case, int[] intArray method is chosen.

In the second case, you have 2 types at the same level, this will cause compilation problems. You cannot have String and StringBuilder because they are at the same level in the class hierarchy. You can have Exception, IOException because they are at different levels.

null can be passed to any method that takes a reference type as argument, but at the same time you need to ensure that you don't have methods that take types that are at the same level.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • @yeppe - `int[]` is also a class which is at a lower level than `Object` class and hence it was chosen. Class hierarchy is `Object` is at top level and all other thigns come below it right? – TheLostMind Sep 23 '16 at 08:40
  • Assuming you meant `Double` and `Float` instead of `double` and `float` (which are primitives), yes, the least generic class will be chosen – TheLostMind Sep 23 '16 at 08:47
  • Primitive array types, also, Object and double primitive array--> method that has double primitive array pram would be selected by the compiler and (Object <>float primitive array) --> method that has float primitive array param and so forth. { compiler picks up the more specific once.} :) – yeppe Sep 23 '16 at 08:59
  • @yeppe - yes, thats right – TheLostMind Sep 23 '16 at 09:00