Java Reference
In-Depth Information
type Point . If you attempt to use an object as a Point object that was actually type Line or type String ,
your program fails.
To avoid such problems, ideally what you need is a LinkedList class that is type safe. By type safe I
mean that when you are using a LinkedList object to store Point objects, no possibility of any other type
of object being added exists. In other words, you want a class that always prevents you from accidentally
adding objects of the wrong type. Of course, you can define a LinkedList class that works only with ob-
jects of type Point . You just use parameters of type Point in the methods that you use to add objects to the
list and to retrieve them. The problem with this solution is that you must write a new LinkedList class for
every type of object that you want to organize in this way, so you end up with a LinkedLineList class, a
LinkedPointList class, a LinkedElephantList class — well, you can see the problem.
That's exactly where generic types come in. Generic types provide a way for you to define a generic
LinkedList class that can transform itself into a class that defines a type safe LinkedList class for objects
of any type that you want to organize in a linked list. Broadly, a generic type can assume the guise of any
particular class from the set or family of classes that it represents. You just supply the appropriate type argu-
ments for the parameters in the generic type and it behaves as that particular class. Let's see how that works
in practice.
DEFINING A GENERIC CLASS TYPE
I'm using the LinkedList class as a model for showing how you define a generic type because you already
know how a linked list works. A definition of a generic class type looks very much like the definition
of an ordinary class, but with a parameter specification added following the class name. Here's how the
LinkedList class from Chapter 6 looks as an outline of a generic type:
public class LinkedList<T> { // T is the type parameter
// Generic type definition...
}
The parameter that appears between the angled brackets, <> , that follows the generic type name,
LinkedList , is called a type parameter . The name, T , identifies the type parameter, and you use the para-
meter name in the definition of the methods and fields in the generic type where there is a dependency on
the argument value for this parameter in the implementation detail. Occurrences of the type parameter name
in the definition of a generic type are called typevariables because they are replaced by a value that is a type
in a similar way to how method parameters are replaced by the arguments that you supply.
Although I've used a single letter, T , as the type parameter name to indicate that the argument should be
a Type , you can use any legal identifier. For example, you could use InsertYourTypeHere as the parameter
name, but this would make the code in the body of the generic type definition rather cumbersome. It's gen-
erally best to keep the parameter names as short as possible — ideally as a single letter. The convention in
Java is to use single letters as type parameter names so it is advantageous to adopt this in your code. Typic-
ally, T is used to indicate a parameter is a type, N is used for a parameter that is a numerical value, K is used
for a parameter that is a key, and V is used for a type parameter that is a value. You read about K and V type
parameters later in this chapter.
Within the text I'm appending angled brackets to a generic type name to differentiate when I'm referring
to a generic type such as LinkedList<> from when I'm referring to an ordinary class or interface type. Al-
though the LinkedList<> example is a generic class type, you can equally well define generic interface
Search WWH ::




Custom Search