Java Reference
In-Depth Information
4.1. A Simple Interface Example
Many simple interfaces define a property that is ascribable to a variety
of different objects from different classes. These properties are often
defined in terms of an object being "able" to do something. For example,
in the standard packages there are a number of "ability" interfaces, such
as:
Cloneable Objects of this type support cloning, as you learned in
detail on page 101 .
Comparable Objects of this type have an ordering that allows them
to be compared.
Runnable Objects of this type represent a unit of work, that can of-
ten execute in an independent thread of control (see Chapter 14 ).
Serializable Objects of this type can be written to an object byte
stream for shipping to a new virtual machine, or for storing per-
sistently and then reconstituting into a live object (see " Object
Serialization " on page 549 ).
Let's look at the Comparable interface in more detail. This interface can be
implemented by any class whose objects can be compared to each other
according to the class's "natural ordering." The interface contains a single
method:
public interface Comparable<T> {
int compareTo(T obj);
}
An interface declaration is similar to a class declaration, except that the
keyword interface is used instead of class . There are also special rules
concerning the members of an interface, as you will soon learn.
 
Search WWH ::




Custom Search