img
// This is wrong!
class MyClass<T extends Comparable<T>>
implements MinMax<T extends Comparable<T>> {
Once the type parameter has been established, it is simply passed to the interface without
further modification.
In general, if a class implements a generic interface, then that class must also be generic,
at least to the extent that it takes a type parameter that is passed to the interface. For example,
the following attempt to declare MyClass is in error:
class MyClass implements MinMax<T> { // Wrong!
Because MyClass does not declare a type parameter, there is no way to pass one to MinMax.
In this case, the identifier T is simply unknown, and the compiler reports an error. Of course,
if a class implements a specific type of generic interface, such as shown here:
class MyClass implements MinMax<Integer> { // OK
then the implementing class does not need to be generic.
The generic interface offers two benefits. First, it can be implemented for different types
of data. Second, it allows you to put constraints (that is, bounds) on the types of data for which
the interface can be implemented. In the MinMax example, only types that implement the
Comparable interface can be passed to T.
Here is the generalized syntax for a generic interface:
interface interface-name<type-param-list> { // ...
Here, type-param-list is a comma-separated list of type parameters. When a generic interface
is implemented, you must specify the type arguments, as shown here:
class class-name<type-param-list>
implements interface-name<type-arg-list> {
Raw Types and Legacy Code
Because support for generics is a recent addition to Java, it was necessary to provide some
transition path from old, pre-generics code. At the time of this writing, there are still millions
and millions of lines of pre-generics legacy code that must remain both functional and
compatible with generics. Pre-generics code must be able to work with generics, and
generic code must be able to work with pre-generic code.
To handle the transition to generics, Java allows a generic class to be used without any
type arguments. This creates a raw type for the class. This raw type is compatible with legacy
code, which has no knowledge of generics. The main drawback to using the raw type is that
the type safety of generics is lost.
Here is an example that shows a raw type in action:
// Demonstrate a raw type.
class Gen<T> {
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home