Java Reference
In-Depth Information
method and the inferred parameter types of the second method
are the same, so the check passes.
3. Since the second method is generic, a second check is then made
to see if the types of the parameters of the first method are sub-
types of the bounds of the generic type parameters of the second
method. Considering the first parameter, the bound of S is Object
and so we are testing whether String is a subtype of Object which
it is. The bound of T , however, is Number and the test is if Object is
a subtype of Number which it is not.
Because the check has failed, the non-generic method is not more spe-
cific than the generic method. Now we must see if the generic method
is more specific than the non-generic one. This proceeds as follows:
1. The second method is not generic this time, so no type inference
occurs.
2. A check is made to see if each parameter of the first method is a
subtype of the type of the corresponding parameter in the second
method. In this case the check is to see whether S is a subtype of
String and T is a subtype of Object . A type variable is a subtype
only of its bounds (and their supertypes) so S is a subtype of Ob-
ject and T is subtype of Number . Because S is a subtype of Object
and not String , the check fails.
The check failed, so the generic method is not more specific than the
non-generic method. Since neither method is more specific than the oth-
er the call is ambiguous and the compiler rejects it as such.
Informally, one method is more specific than another if all calls to the
first method could be handled by the second. Looking at the two meth-
ods here, the most general signature of the generic method would have
parameters of type Object and Number . So if we consider the first para-
meter, anything we pass to the non-generic method must be a String ,
and a String is always an Object , so the generic method could accept all
first arguments to the first method. So considering just the first para-
meter, the non-generic method is more specific than the generic meth-
od. But conversely, the generic method can accept first arguments that
 
Search WWH ::




Custom Search