Java Reference
In-Depth Information
4.2. Interface Declarations
An interface is declared using the keyword
interface
, giving the interface
a name and listing the interface members between curly braces.
An interface can declare three kinds of members:
•
constants (fields)
•
methods
•
nested classes and interfaces
All interface members are implicitly public, but, by convention, the
public
modifier is omitted. Having non-public members in an interface would
make little sense; where it does make sense you can use the accessibility
of the interface itself to control access to the interface members.
4.2.1. Interface Constants
An interface can declare named constants. These constants are defined
as fields but are implicitly
public
,
static
, and
final
again, by convention,
the modifiers are omitted from the field declarations. These fields must
also have initializers
blank finals
are not permitted. Annotations can also
be applied to the fieldssee
Chapter 15
.
Because interfaces contain no implementation details, they cannot define
normal fieldssuch a definition would be dictating implementation policy to
the classes that choose to implement the interface. Interfaces can define
named constants because these are useful in the design of types. For ex-
ample, an interface that had differing levels of verbosity in its contract
might have the following:
interface Verbose {
int SILENT = 0;