Note: In these exercises, the names List
,
ArrayList
, Collections
, and Vector
should be taken as imported from java.util
.
Exercise 2.x3: One can show the subtype relation on Java types by
drawing a diagram with arrows, where each arrow points from a subtype
to one of its supertypes. Arrows can be omitted that are implied by transitivity. As a simple example, if
the types under consideration are Integer
, Number
, and Object
,
one would draw an arrow pointing from Integer
to
Number
and a second arrow pointing from
Number
to Object
, but there would be no
need to show an arrow from Integer
directly to
Object
. Using this diagramming convention, show the
subtyping relation on List<?>
,
ArrayList<?>
,
List<Number>
, ArrayList<Number>
,
List<Integer>
, and ArrayList<Integer>
.
(Note: This problem was later rescinded.) Exercise 2.x4: Look up the "signature" of Collections.fill
in the Java API, that is, the full declaration of this method
including the type of arguments. You should find that the type of the
first argument is a
bounded wildcard, in which the type parameter T
serves as
a bound for the ?
wildcard. Is T
serving
here as an upper bound or a lower bound?
Consider what would happen if this bounded wildcard
were replaced by T
itself, so that the first argument to
Collections.fill
would have type
List<T>
.
Give an example snippet of code that compiles
correctly with the API as it stands, but would result in a
compile-time error if the API were changed in this way.
Exercise 3.x1: The definition of the typing
method
on page 52 of the Tucker and Noonan text is based on a definition of
Declarations
(given in Appendix B) as a subclass of
Vector
. Suppose that Appendix B were changed to take
advantage of Java Generics, so that
Declarations
is now a subclass of
Vector<Declaration>
. Show how the code for
typing
could be simplified. First show the
simplification that results just from taking advantage of this more
specific type. Then show the further simplification from making use
of the newly available style of for
loop.