Java Reference
In-Depth Information
SYNTAX (FOR CLASS DEFINITION HEADINGS)
public class
Class_Name
<
Type
extends
Ancestor_Class
&
Interface_1
&
Interface_2
& ...
...&
Last_Interface
>
If there are multiple type parameters, they are separated by commas. There can be any
number of interfaces but only one ancestor class for each type parameter.
EXAMPLES
public class
Pair<T
extends
Comparable>
public class
MyClass<T
extends
Employee & Comparable>
public class
YourClass <T1
extends
Employee & Comparable
& Cloneable, T2
extends
Comparable>
Employee
is a class.
Comparable
and
Cloneable
are interfaces.
TIP: Generic Interfaces
An interface can have one or more type parameters. The details and notation are the
same as they are for classes with type parameters.
■
Generic Methods
★
(This is a starred subsection because it is needed only for Chapter 16, which covers
the Java collection classes. If you choose to read Chapter 16, you will need to read this
subsection first.)
When you define a generic class, you can use the type parameter in the definitions
of the methods for that generic class. You also can define a generic method that has its
own type parameter that is not the type parameter of any class. This generic method
can be a member of an ordinary (nongeneric) class or a member of some generic class
with some other type parameter. For example,
public class
Utility
{
...
public static
<T> T getMidpoint(T[] a)
{
return
a[a.length/2];
}
public static
<T> T getFirst(T[] a)
{
return
a[0];
}
...
}