4
public class Test
 {

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

    public Test(int[] o) {
        System.out.println("array");
    }

    public static void main(String[] args) {
        new Test(null);
    }
}

Output :

array

Can somebody please explain me the reason behind this?

1

5 Answers 5

7

The rule is: the more specific method will be called. In this case, it's the method receiving a int[] which is more specific than the one receiving a java.lang.Object parameter.

Here's a link to Java's official documentation referencing to this case. Take a look at section 15.2.2. Quoting an interesting part (section 15.2.2.5):

If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.

The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.

An another one:

It is possible that no method is the most specific, because there are two or more methods that are maximally specific. In this case:

If all the maximally specific methods have override-equivalent (§8.4.2) signatures, then: If exactly one of the maximally specific methods is not declared abstract, it is the most specific method. Otherwise, if all the maximally specific methods are declared abstract, and the signatures of all of the maximally specific methods have the same erasure (§4.6), then the most specific method is chosen arbitrarily among the subset of the maximally specific methods that have the most specific return type. However, the most specific method is considered to throw a checked exception if and only if that exception or its erasure is declared in the throws clauses of each of the maximally specific methods. Otherwise, we say that the method invocation is ambiguous, and a compile-time error occurs.

Sign up to request clarification or add additional context in comments.

3 Comments

Just for completenes: Why is int[] more specific than Object?
@LutzHorn because java.lang.Object is the root of all Java types by definition - hence it is the least specific type possible (everything extends java.lang.Object)
@LutzHorn NOTE: Primitive types such as int, long etc. are another story of course, so above "everything" is wrong in the context ;))
1

It's always subclass would be preferred over the parent class. If you want to call Object one you could do the following:

new goFuncTest((Object) null);

Comments

1

As pablo's answer suggests, the most specific / least generic method will be called. Actually, the classlevel in the class hierarchy is checked to decide which method to call. The method with lower class level is called.

If 2 classes are at the same level, then you will get a compile time error stating - ambiguous method call just like in the below case.

public class Sample {
void someMethod(Object o) {
    System.out.println("object");
}

void someMethod(String s) {
    System.out.println("String");
}

void someMethod(int[] o) {
    System.out.println("int[]");
}

public static void main(String[] args) {
    new Sample().someMethod(null); // error ambiguous call both array and String are direct children of Object
}

Case -2 : To prove that the method with lower class level will be called.

public class Sample {
void someMethod(Object o) {
    System.out.println("object");
}

void someMethod(OutputStream os) {
    System.out.println("OutputStream");
}

void someMethod(FileOutputStream fos) {
    System.out.println("FileOutputStream");
}

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

}

O/P :FileOutputStream

Since FileOutputStream is child of OutputStream which is again a child of Object class (FileOutputStream is also a child of both OutputStream and Object classes). So the compiler goes like this Object --> OutputStream --> FileOutputStream while resolving the method call.

Comments

0

Thumb rule is child first in method call. As Object is parent of all and as null can be assigned to any type of object instance variable, it will first match any java type(which of course is child of object) and then super parent i.e. Object.

Comments

0

up gradation of Null to Array is preferred over casting it to Object. Object is top in the hierarchy while Array is closest to Null.

This is like up gradation of byte to int.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.