开发者

How does polymorph ambiguity distinction work?

开发者 https://www.devze.com 2022-12-27 01:05 出处:网络
Given I have a class wi开发者_Python百科th two constructors: public class TestClass { ObjectOne o1;

Given I have a class wi开发者_Python百科th 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?


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

  • Polymorphism vs Overriding vs Overloading
  • Method Overloading. Can you overuse it?


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);


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.


It will not compile, obviously.


Compile error: The constructor TestClass is ambiguous


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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号