O'Reilly logo

Just Java™ 2 by Peter van der Linden

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Generic Interfaces

Interfaces, as well as classes, can be parameterized with generic types. As you'd expect, it looks exactly the same as when a class is parameterized. We'll walk through a complete “before and after” example here, because we'll use it in the next section as part of a more complicated generic parameter declaration.

This example uses the standard java.lang.Comparable interface type that defines a compareTo() method used for comparing two objects. The method returns an int which is negative, zero, or positive to indicate this is smaller, equal, or bigger than the argument.

java.lang.Comparable without generics

The Comparable interface looks like this in JDK 1.4:

public interface Comparable {    int compareTo(Object obj) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required