Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does polymorph ambiguity distinction work?

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?

like image 476
Peter Wippermann Avatar asked Nov 28 '22 11:11

Peter Wippermann


2 Answers

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);
like image 26
mkorpela Avatar answered Dec 05 '22 06:12

mkorpela


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?
like image 162
polygenelubricants Avatar answered Dec 05 '22 06:12

polygenelubricants