to define a type without defining a class. Interfaces define types in an abstract form as a collection of methods
or other types that form the contract for that type. Interfaces contain no implementation and you cannot create
instances of an interface. Rather, classes can expand their own types by implementing one or more interfaces.
An interface is an expression of pure design, whereas a class is a mix of design and implementation.
A class can implement the methods of an interface in any way that the designer of the class chooses. An
interface thus has many more possible implementations than a class. Every major class in an application
should be an implementation of some interface that captures the contract of that class.
Classes can implement more than one interface. The Java programming language allows multiple inheritance
of interface but only single inheritance of implementationa class can extend only one other class. Classes can
use inheritance of interfaces to expand their type and then use, for example, composition to provide an
implementation for those interfaces. This design allows the typing flexibility of multiple inheritance while
avoiding the pitfalls of multiple implementation inheritance, at the cost of some additional work for the
programmer.
In a given class, the classes that are extended and the interfaces that are implemented are collectively called
the supertypes, and from the viewpoint of the supertypes, the new class is a subtype. The new class includes
all its supertypes, so a reference to an object of the subtype can be used polymorphically anywhere a reference
to an object of any of its supertypes (class or interface) is required. Interface declarations create type names
just as class declarations do; you can use the name of an interface as the type name of a variable, and any
object whose class implements that interface can be assigned to that variable.
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 often 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 persistently 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
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.
The compareTo method takes a single object argument of type T and compares it to the current object
(expected to also be of type T), returning a negative, zero, or positive integer if the current object is less than,
equal to, or greater than the argument, respectively.