The optional implements clause in a class declaration lists the names of interfaces that are direct superinterfaces of the class being declared.
Superinterfaces:
implements InterfaceTypeList
InterfaceTypeList:
InterfaceType {, InterfaceType}
Each InterfaceType must name an accessible interface type (§6.6), or a compile-time error occurs.
If an InterfaceType has type arguments, it must denote a well-formed parameterized type (§4.5), and none of the type arguments may be wildcard type arguments, or a compile-time error occurs.
It is a compile-time error if the same interface is mentioned as a direct superinterface more than once in a single implements clause. This is true even if the interface is named in different ways.
Example 8.1.5-1. Illegal Superinterfaces
class Redundant implements java.lang.Cloneable, Cloneable {
int x;
}
This program results in a compile-time error because the names java.lang.Cloneable and Cloneable refer to the same interface.
Given a (possibly generic) class declaration C<F_1,...,F_n> (n ≥ 0, C ≠ Object), the direct superinterfaces of the class type C<F_1,...,F_n> are the types given in the implements clause of the declaration of C, if an implements clause is present.
Given a generic class declaration C<F_1,...,F_n> (n > 0), the direct superinterfaces of the parameterized class type C<T_1,...,T_n>, where T_i (1 ≤ i ≤ n) is a type, are all types I<U_1 θ,...,U_k θ>, where I<U_1,...,U_k> is a direct superinterface of C<F_1,...,F_n> and θ is the substitution [F_1:=T_1,...,F_n:=T_n].
An interface type I is a superinterface of class type C if any of the following is true:
- I is a direct superinterface of C.
- C has some direct superinterface J for which I is a superinterface, using the definition of "superinterface of an interface" given in §9.1.3.
- I is a superinterface of the direct superclass of C.
A class can have a superinterface in more than one way.
A class is said to implement all its superinterfaces.
A class may not at the same time be a subtype of two interface types which are different parameterizations of the same generic interface (§9.1.2), or a subtype of a parameterization of a generic interface and a raw type naming that same generic interface, or a compile-time error occurs.
This requirement was introduced in order to support translation by type erasure (§4.6).
Example 8.1.5-2. Superinterfaces
interface Colorable {
void setColor(int color);
int getColor();
}
enum Finish { MATTE, GLOSSY }
interface Paintable extends Colorable {
void setFinish(Finish finish);
Finish getFinish();
}
class Point { int x, y; }
class ColoredPoint extends Point implements Colorable {
int color;
public void setColor(int color) { this.color = color; }
public int getColor() { return color; }
}
class PaintedPoint extends ColoredPoint implements Paintable {
Finish finish;
public void setFinish(Finish finish) {
this.finish = finish;
}
public Finish getFinish() { return finish; }
}
Here, the relationships are as follows:
- The interface
Paintableis a superinterface of classPaintedPoint. - The interface
Colorableis a superinterface of classColoredPointand of classPaintedPoint. - The interface
Paintableis a subinterface of the interfaceColorable, andColorableis a superinterface ofPaintable, as defined in §9.1.3.
The class PaintedPoint has Colorable as a superinterface both because it is a superinterface of ColoredPoint and because it is a superinterface of Paintable.
Example 8.1.5-3. Illegal Multiple Inheritance of an Interface
interface I<T> {}
class B implements I<Integer> {}
class C extends B implements I<String> {}
Class C causes a compile-time error because it attempts to be a subtype of both I<Integer> and I<String>.
Unless the class being declared is abstract, all the abstract member methods of each direct superinterface must be implemented (§8.4.8.1) either by a declaration in this class or by an existing method declaration inherited from the direct superclass or a direct superinterface, because a class that is not abstract is not permitted to have abstract methods (§8.1.1.1).
Each default method (§9.4.3) of a superinterface of the class may optionally be overridden by a method in the class; if not, the default method is typically inherited and its behavior is as specified by its default body.
It is permitted for a single method declaration in a class to implement methods of more than one superinterface.
Example 8.1.5-3. Implementing Methods of a Superinterface
interface Colorable {
void setColor(int color);
int getColor();
}
class Point { int x, y; };
class ColoredPoint extends Point implements Colorable {
int color;
}
This program causes a compile-time error, because ColoredPoint is not an abstract class but fails to provide an implementation of methods setColor and getColor of the interface Colorable.
In the following program:
interface Fish { int getNumberOfScales(); }
interface Piano { int getNumberOfScales(); }
class Tuna implements Fish, Piano {
// You can tune a piano, but can you tuna fish?
public int getNumberOfScales() { return 91; }
}
the method getNumberOfScales in class Tuna has a name, signature, and return type that matches the method declared in interface Fish and also matches the method declared in interface Piano; it is considered to implement both.
On the other hand, in a situation such as this:
interface Fish { int getNumberOfScales(); }
interface StringBass { double getNumberOfScales(); }
class Bass implements Fish, StringBass {
// This declaration cannot be correct,
// no matter what type is used.
public ?? getNumberOfScales() { return 91; }
}
it is impossible to declare a method named getNumberOfScales whose signature and return type are compatible with those of both the methods declared in interface Fish and in interface StringBass, because a class cannot have multiple methods with the same signature and different primitive return types (§8.4). Therefore, it is impossible for a single class to implement both interface Fish and interface StringBass (§8.4.8).