Java Reference
In-Depth Information
double getCenterX ();
double getCenterY ();
}
A number of restrictions apply to the members of an interface:
• All mandatory methods of an interface are implicitly abstract and must have a
semicolon in place of a method body. The abstract modifier is allowed, but by
convention is usually omitted.
• An interface defines a public API. All members of an interface are implicitly
public , and it is conventional to omit the unnecessary public modifier. It is a
compile-time error to try to define a protected or private method in an
interface.
• An interface may not define any instance fields. Fields are an implementation
detail, and an interface is a specification not an implementation. The only fields
allowed in an interface definition are constants that are declared both static
and final .
• An interface cannot be instantiated, so it does not define a constructor.
• Interfaces may contain nested types. Any such types are implicitly public and
static . See “Nested Types” on page 155 for a full description of nested types.
• As of Java 8, an interface may contain static methods. Previous versions of Java
did not allow this, and this is widely believed to have been a flaw in the design
of the Java language.
m
e
Extending Interfaces
Interfaces may extend other interfaces, and, like a class definition, an interface defi‐
nition may include an extends clause. When one interface extends another, it
inherits all the methods and constants of its superinterface and can define new
methods and constants. Unlike classes, however, the extends clause of an interface
definition may include more than one superinterface. For example, here are some
interfaces that extend other interfaces:
interface Positionable extends Centered {
void setUpperRightCorner ( double x , double y );
double getUpperRightX ();
double getUpperRightY ();
}
interface Transformable extends Scalable , Translatable , Rotatable {}
interface SuperShape extends Positionable , Transformable {}
An interface that extends more than one interface inherits all the methods and con‐
stants from each of those interfaces and can define its own additional methods and
constants. A class that implements such an interface must implement the abstract
methods defined directly by the interface, as well as all the abstract methods inher‐
ited from all the superinterfaces.
Search WWH ::




Custom Search