implements
clause in a class declaration lists the names of interfaces that are direct superinterfaces of the class being declared:
Interfaces:The following is repeated to make the presentation here clearer:
implements
InterfaceTypeList InterfaceTypeList:
InterfaceType
InterfaceTypeList,
InterfaceType
InterfaceType:Each InterfaceType must name an accessible interface type, or a compile- time error occurs. All interfaces in the current package are accessible. Interfaces in other packages are accessible if the host system permits access to the package and the interface is declared
TypeName
public
.
A compile-time error occurs if the same interface is mentioned two or more times in a single implements
clause, even if the interface is named in different ways; for example, the code:
class Redundant implements java.lang.Cloneable, Cloneable { int x; }results in a compile-time error because the names
java.lang.Cloneable
and
Cloneable
refer to the same interface.
An interface type I is a superinterface of class type C if any of the following is true:
public interface Colorable { void setColor(int color); int getColor(); }the relationships are as follows:
public interface Paintable extends Colorable { int MATTE = 0, GLOSSY = 1; void setFinish(int finish); int 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
{ int finish; public void setFinish(int finish) { this.finish = finish; } public int getFinish() { return finish; } }
Paintable
is a superinterface of class PaintedPoint
.
Colorable
is a superinterface of class ColoredPoint
and of class PaintedPoint
.
Paintable
is a subinterface of the interface Colorable
, and Colorable
is a superinterface of Paintable
, a
s.
PaintedPoint
has Colorable
as a superinterface both because it is a superinterface of ColoredPoint
and because it is a superinterface of Paintable
.
Unless the class being declared is abstract
, the declarations of the methods defined in each direct superinterface must be implemented either by a declaration in this class or by an existing method declaration inherited from the direct superclass, because a class that is not abstract
is not permitted to have abstract
methods.
interface Colorable { void setColor(int color); int getColor(); }causes a compile-time error, because
class Point { int x, y; };
class ColoredPoint extends Point implements Colorable { int color; }
ColoredPoint
is not an abstract
class but
it fails to provide an implementation of methods setColor
and getColor
of the
interface Colorable
.
It is permitted for a single method declaration in a class to implement methods of more than one superinterface. For example, in the code:
interface Fish { int getNumberOfScales(); }
interface Piano { int getNumberOfScales(); }
class Tuna implements Fish, Piano { // You can tune a piano, but can you tuna fish? 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
with the same
signature and return type as those of both the methods declared in interface Fish
and in interface StringBass
, because a class can have only one method with a
given signature. Therefore, it is impossible for a single class to implement
both interface Fish
and interface StringBass
.