Java Reference
In-Depth Information
patible 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 ex-
ception classes.
Continuing Your Study of Generics
As mentioned at the start, this chapter gives you sufficient knowledge to use generics ef-
fectively 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 Java: The Complete Reference,
Ninth Edition (Oracle Press/McGraw-Hill Professional, 2014).
Chapter 13 Self Test
1 . Generics are important to Java because they enable the creation of code that is
A. Type-safe
B. Reusable
C. Reliable
D. All of the above
2 . Can a primitive type be used as a type argument?
3 . Show how to declare a class called FlightSched that takes two generic parameters.
Search WWH ::




Custom Search