Java Reference
In-Depth Information
casts (as determined by the type arguments) to maintain type compatibility with the types
specified by the type arguments. The compiler also enforces this type compatibility. This
approach to generics means that no type parameters exist at run time. They are simply a
source-code mechanism.
Ambiguity Errors
The inclusion of generics gives rise to a new type of error that you must guard against:
ambiguity . Ambiguity errors occur when erasure causes two seemingly distinct generic de-
clarations to resolve to the same erased type, causing a conflict. Here is an example that
involves method overloading:
Notice that MyGenClass declares two generic types: T and V . Inside MyGenClass , an
attempt is made to overload set( ) based on parameters of type T and V . This looks reas-
onable because T and V appear to be different types. However, there are two ambiguity
problems here.
First, as MyGenClass is written there is no requirement that T and V actually be dif-
ferent types. For example, it is perfectly correct (in principle) to construct a MyGenClass
object as shown here:
Search WWH ::




Custom Search