Java Reference
In-Depth Information
EXERCISES
You can download the source code for the examples in the topic and the solutions to the following exer-
cises from www.wrox.com .
1. A stack is a container that stores objects in a manner indicated by its name — a vertical stack where
only the object at the top of the stack is accessible. It works rather like a sprung stack of plates in a
cafeteria. Only the top plate is at counter level and, therefore, is the only one you can access. When you
add a plate to the stack, the existing plates are pushed down so the new plate is now the one that you
can access. Define a generic Stack<> type with a method push() that adds the object that is passed
as an argument to the top of the stack, and with a method pop() that removes and returns the object
that is currently at the top of the stack. The pop() method should return null when the stack is empty.
Demonstrate the operation of your Stack<> implementation by storing and retrieving 10 strings and
10 Double objects in stacks of a suitable type.
2. Implement and demonstrate a listAll() method in the Stack<> class definition that will list the
objects in the stack.
3. Modify your Stack<> type to make it serializable. Demonstrate that this is the case by creating a
Stack<String> object, adding 10 strings to it, serializing and deserializing the Stack<String> ob-
ject, and listing the contents of the deserialized stack.
• WHAT YOU LEARNED IN THIS CHAPTER
TOPIC
CONCEPT
Generic
Types
A generic type, which is also referred to as a parameterized type, defines a family of classes or inter-
faces using one or more type parameters. Container classes are typically defined as generic types.
Using a Gen-
eric Type
You define a specific type from a generic type by supplying a type argument for each type parameter.
You can omit the type arguments for the type parameters in a constructor call for a generic type and the
compiler infers the parameter arguments from the type of variable you use to store the reference.
Generic Type
Arguments
The argument that you supply for a type parameter can be a class type or an interface type. It cannot be
a primitive type.
Type Para-
meter Bounds
You can limit the scope of type arguments for a given type parameter by specifying one or more bounds
for the parameter using the extends keyword. The first bound can be a class or interface type; the
second and subsequent bounds must be interface types.
Runtime Type
of Generic
Type In-
stances
All types produced from a given generic type share the same runtime type.
Parameterized
Methods
A parameterized method defines a family of methods using one or more independent type parameters. A
parameterized method can be a member of an ordinary class type or a generic type.
Wildcard
Type Argu-
ments
A wildcard type argument matches any type. This allows you to specify a method parameter type as any
instance of a generic type. For example a parameter of type Iterator<?> accepts a reference of type
Iterator<T> type for any T .
Search WWH ::




Custom Search