Java Reference
In-Depth Information
are not String objects, so the generic method is not more specific than
the non-generic method. Considering the second parameter, you can
pass arbitrary objects to the non-generic method but only Number ob-
jects to the generic method. Consequently, the non-generic method is
not more specific at the second parameter and so it is not more specific.
As neither is more specific, the call is ambiguous.
You can remove the ambiguity in two ways. You can cast the first argu-
ment to Object so that the non-generic method is no longer applicable
and so the generic version must be called:
m((Object) "hello", Integer.valueOf(29));
The alternative is to cast the second argument to Object so that the gen-
eric method is no longer applicable and so the non-generic version must
be called:
m("hello", (Object) Integer.valueOf(29));
Note that parameterizing the invocation itself does not automatically ex-
clude the non-generic methodits applicability is determined without con-
sideration of the type arguments.
Overloading, even without involving generics, should be used judiciously
and only to improve the resulting clarity of the program. With generics
added to the mix, it is even easier to create programs in which the intent
can only be established by detailed recourse to the language specifica-
tion. Avoid mixing generic and non-generic overloads of a method un-
less you have an extremely compelling reason.
 
Search WWH ::




Custom Search