System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə62/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   58   59   60   61   62   63   64   65   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

ambiguity. Ambiguity errors occur when erasure causes two seemingly distinct generic
declarations 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
reasonable 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
different types. For example, it is perfectly correct (in principle) to construct a
MyGenClass object as shown here:
In this case, both T and V will be replaced by String. This makes both versions of set(
) identical, which is, of course, an error.
Second, and more fundamental, is that the type erasure of set( ) effectively reduces
both versions to the following:
Thus, the overloading of set( ) as attempted in MyGenClass is inherently ambiguous.
The solution in this case is to use two separate method names rather than trying to
overload set( ).
SOME GENERIC RESTRICTIONS


SOME GENERIC RESTRICTIONS
There are a few restrictions that you need to keep in mind when using generics. They
involve creating objects of a type parameter, static members, exceptions, and arrays.
Each is examined here.
Type Parameters Can’t Be Instantiated
It is not possible to create an instance of a type parameter. For example, consider this
class:
Here, it is illegal to attempt to create an instance of T. The reason should be easy to
understand: the compiler has no way to know what type of object to create. T is simply
a placeholder.
Restrictions on Static Members
No static member can use a type parameter declared by the enclosing class. For
example, both of the static members of this class are illegal:
Although you can’t declare static members that use a type parameter declared by the
enclosing class, you can declare static generic methods, which define their own type
parameters, as was done earlier in this chapter.
Generic Array Restrictions


Generic Array Restrictions
There are two important generics restrictions that apply to arrays. First, you cannot
instantiate an array whose element type is a type parameter. Second, you cannot create
an array of type­specific generic references. The following short program shows both
situations:
As the program shows, it’s valid to declare a reference to an array of type T, as this line
does:
But, you cannot instantiate an array of T, as this commented­out line attempts:
The reason you can’t create an array of T is that there is no way for the compiler to
know what type of array to actually create. However, you can pass a reference to a type­


compatible array to Gen( ) when an object is created and assign that reference to vals,
as the program does in this line:
This works because the array passed to Gen() has a known type, which will be the same
type as T at the time of object creation. Inside main( ), notice that you can’t declare an
array of references to a specific generic type. That is, this line
won’t compile.
Generic Exception Restriction
A generic class cannot extend Throwable. This means that you cannot create generic
exception classes.
CONTINUING YOUR STUDY OF GENERICS
As mentioned at the start, this chapter gives you sufficient knowledge to use generics
effectively in your own programs. However, there are many side issues and special
cases that are not covered here. Readers especially interested in generics will want to
learn about how generics affect class hierarchies, run­time type comparisons, and
overriding, for example. Discussions of these and other topics are found in my book

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   58   59   60   61   62   63   64   65   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə