views:

154

answers:

6

Given I have a class with two constructors:

public class TestClass {
    ObjectOne o1;
    ObjectTwo o2;

    public TestClass(ObjectOne o1) {
        // ..
    }

    public TestClass(ObjectTwo o2) {
        // ..
    }
}

Please assume, that ObjectOne is an interface type, and ObjectTwo implements ObjectOne. What happens, if I call:

new TestClass(null);

How to determine the correct method to call? And who determines that? Are there differences between Java and other OOP languages?

+5  A: 

There is no magic. You have to cast the null parameter either to ObjectOne or ObjectTwo.

So:

 new TestClass((ObjectOne) null);

or

 new TestClass((ObjectTwo) null);
mkorpela
Actually, there is some "magic". See my answer (with references to JLS).
polygenelubricants
+1  A: 

The compiler will fail because you are making an ambiguous call to the constructor. The C# compiler has the same behavior.

In order to make this work in either language you must cast the null to either one of the two types to disambiguate the call to the constructor.

Andrew Hare
+1  A: 

It will not compile, obviously.

nunthrey
Actually, there are _plenty_ of scenarios in which it will compile. See my answer for details.
polygenelubricants
next time instead of answering the same thing as someone-else, consider voting for his answer. Does not sound like much but voting instead of duplicating answers is partly what makes SO the gold mine that it is. Duplicating answers just adds clutter and dilutes the better answers. my 2 cent
Newtopian
+1  A: 

Compile error: The constructor TestClass is ambiguous

Michał Mech
next time instead of answering the same thing as someone-else, consider voting for his answer. Does not sound like much but voting instead of duplicating answers is partly what makes SO the gold mine that it is. Duplicating answers just adds clutter and dilutes the better answers. my 2 cent
Newtopian
You should really check the answers times. Everyone was writing in the same time. I didn't know that someone else is writing the same answer.
Michał Mech
+11  A: 

This question is really about resolving ambiguous overloads, and not really about run-time polymorphism (because choosing which overloaded method to invoke is done at compile-time).

Method resolution is a complicated thing, and in this case, whether or not the code compiles at all does depend on what the types involved are. There are plenty of situations in which the code will compile. See code below (note that String implements CharSequence):

public class MyClass {
    MyClass(CharSequence charSeq) {
        System.out.println("CharSequence");
    }
    MyClass(String s) {
        System.out.println("String");
    }   
    public static void main(String[] args) {
        new MyClass(null); // prints "String"
        new MyClass(""); // prints "String"
        new MyClass((CharSequence) null); // prints "CharSequence"
        new MyClass((CharSequence) "");   // prints "CharSequence"
    }
}

Note that without the cast, the String overload is chosen. This is exactly as specified in JLS:

JLS 15.12.2.5 Choosing the Most Specific Method

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.

A String is-a CharSequence, but not all CharSequence is-a String. Therefore, String is more specific than CharSequence, hence why the String overload is chosen in the above example.


It is worth noting that this exact question (in essence) appeared in the wonderful Java Puzzlers (highly recommended), specifically Puzzle 46: The Case of the Confusing Constructor

Java's overload resolution process operates in two phases. The first phase selects all the methods or constructors that are accessible and applicable. The second phase selects the most specific of the methods or constructors selected in the first phase. One method or constructor is less specific than another if it can accept any parameters passed to the other

The key to understanding this puzzle is that the test for which method or constructor is most specific does not use the actual parameters: the parameters appearing in the invocation. They are used only to determine which overloadings are applicable. Once the compiler determines which overloadings are applicable and accessible, it selects the most specific overloading, using only the formal parameters: the parameters appearing in the declaration.


I will close with a quote from Effective Java 2nd Edition, Item 41: Use overloading judiciously:

The rules that determine which overloading is selected are extremely complex. They take up thirty-three pages in the language specification, and few programmers understand all of their subtleties.

Needless to say, this book is also highly recommended.

See also

polygenelubricants
+1 for teaching me something interesting.
mkorpela
Thank you very much for this comprehensive and detailled answer. I really appreciate for the time you put into this :-)
Peter Wippermann
A: 

I don't know if cast will work. You can try this :

ObjectOne o1 = null;
new TestClass(o1);
fastcodejava