Java Reference
In-Depth Information
If a class needs several such services, it has to implement several interfaces.
To do this, simply write the name of the interfaces (separated by commas) after
keyword implements . For example, class G in Fig. 12.1 could implement not
only I but also Comparable by changing its first line to:
public class G extends F implements I, Comparable {
Of course, class G would then have to implement method compareTo .
Figure 12.3, on the left, shows how we draw the new class G using our
dimensional model. As before, there are dimensions that contain ( G , F , Object )
and ( G , I ). But there is now a third dimension, which contains ( G , Comparable ).
Upward casts in any dimension are performed automatically, when neces-
sary. Downward casts must be explicitly stated. And at any point, the components
that can be accessed are those at that point and upward in that dimension.
Extending an interface
Suppose we have one more interface, named S :
public interface S {
void pr( int b);
Java allows one interface to extend another, so suppose that interface I extends
interface S . Then our dimensional diagram is as shown to the right in Fig. 12.3.
The dimension that contains I is extended to include this new interface.
The form of the extends clause within an interface definition is:
extends interface-name , ... , interface-name
In keeping with the subclass-superclass terminology, we say that S is a
superinterface of I and I is a subinterface of S .
Object: ...
Object: ...
Comparable: compareTo
Comparable: compareTo
F: ...
F: ... S: ...
I: p(C)
I: p(C)
G: b
G() p(C)
G: b
G() p(C)
Figure 12.3:
Extending two interfaces and an interface extending an interface
Search WWH ::

Custom Search