📄 9.doc.html
字号:
<html>
<head>
<title>The Java Language Specification Interfaces</title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="8.doc.html">Prev</a> | <a href="10.doc.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="238678"></a>
<p><strong>
CHAPTER 9 </strong></p>
<a name="238680"></a>
<h1>Interfaces</h1>
<hr><p>
<a name="236346"></a>
An interface declaration introduces a new reference type whose members are
constants and abstract methods. This type has no implementation, but otherwise
unrelated classes can implement it by providing implementations for its abstract
methods.
<p><a name="236347"></a>
Java programs can use interfaces to make it unnecessary for related classes to share a common abstract superclass or to add methods to <code>Object</code>.<p>
<a name="19956"></a>
An interface may be declared to be an <i>direct extension </i>of one or more other interfaces, meaning that it implicitly specifies all the abstract methods and constants of the interfaces it extends, except for any constants that it may hide.<p>
<a name="30809"></a>
A class may be declared to <i>directly implement</i> one or more interfaces, meaning that any instance of the class implements all the abstract methods specified by the interface or interfaces. A class necessarily implements all the interfaces that its direct superclasses and direct superinterfaces do. This (multiple) interface inheritance allows objects to support (multiple) common behaviors without sharing any implementation.<p>
<a name="35467"></a>
A variable whose declared type is an interface type may have as its value a reference to any object that is an instance of a class declared to implement the specified interface. It is not sufficient that the class happen to implement all the abstract methods of the interface; the class or one of its superclasses must actually be declared to implement the interface, or else the class is not considered to implement the interface.<p>
<a name="35470"></a>
<h2>9.1 Interface Declarations</h2>
<a name="27727"></a>
An interface declaration specifies a new reference type:
<p><ul><pre>
<i>InterfaceDeclaration:<br>
</i> <i>InterfaceModifiers</i><sub><i>opt</i></sub><code> interface </code><i>Identifier<br>
</i> <i>ExtendsInterfaces</i><sub><i>opt</i></sub><code> </code><i>InterfaceBody
</i></pre></ul><a name="235931"></a>
A compile-time error occurs if the <em>Identifier </em>naming an interface appears as the
name of any other class or interface in the same package. A compile-time error
also occurs if the <i>Identifier</i> naming an interface appears as the name by which a
class or interface is to be known via a single-type-import declaration <a href="7.doc.html#26699">(§7.5.1)</a> in
the compilation unit containing the interface declaration. In the example:
<p><pre><br><a name="17005"></a>class Point { int x, y; }
<br><a name="17006"></a>interface Point { void move(int dx, int dy); }
</pre><a name="17007"></a>
a compile-time error occurs because a <code>class</code> and an <code>interface</code> in the same package
cannot have the same name.
<p><a name="39713"></a>
<h3>9.1.1 Scope of an Interface Type Name</h3>
<a name="235941"></a>
The <i>Identifier</i> specifies the name of the interface and has as its scope the entire
package in which it is declared. This is the same scoping rule as for class type
names; see <a href="8.doc.html#39196">§8.1.1</a> for an example involving classes.
<p><a name="235947"></a>
<h3>9.1.2 Interface Modifiers</h3>
<a name="17033"></a>
An interface declaration may be preceded by <i>interface modifiers</i>:
<p><ul><pre>
<i>InterfaceModifiers:<br>
</i><code> </code><i>InterfaceModifier<br>
</i><code> </code><i>InterfaceModifiers</i><code> </code><i>InterfaceModifier
</i>
<i>InterfaceModifier:</i> <i>one</i> <i>of<br>
</i><code> public abstract
</code></pre></ul><a name="36030"></a>
The access modifier <code>public</code> is discussed in <a href="6.doc.html#33916">§6.6</a>. A compile-time error occurs if
the same modifier appears more than once in an interface declaration.
<p><a name="30820"></a>
<h4>9.1.2.1 abstract Interfaces</h4>
<a name="30821"></a>
Every interface is implicitly <code>abstract</code>. This modifier is obsolete and should not
be used in new Java programs.
<p><a name="78598"></a>
<h3>9.1.3 Superinterfaces</h3>
<a name="78599"></a>
If an <code>extends</code> clause is provided, then the interface being declared extends each
of the other named interfaces and therefore inherits the methods and constants of
each of the other named interfaces. These other named interfaces are the <i>direct
superinterfaces</i> of the interface being declared. Any class that <code>implements</code> the
declared interface is also considered to implement all the interfaces that this interface
<code>extends</code> and that are accessible to the class.
<p><ul><pre>
<i>ExtendsInterfaces:<br>
</i> <code>extends </code><i>InterfaceType<br>
</i> <i>ExtendsInterfaces</i><code> , </code><i>InterfaceType
</i></pre></ul><a name="21599"></a>
The following is repeated from <a href="4.doc.html#9317">§4.3</a> to make the presentation here clearer:
<p><ul><pre>
<i>InterfaceType:<br>
</i> <i>TypeName
</i></pre></ul><a name="152015"></a>
Each <i>InterfaceType </i>in the <code>extends</code> clause of an interface declaration must name an accessible interface type; otherwise a compile-time error occurs.<p>
<a name="152016"></a>
A compile-time error occurs if there is a circularity such that an interface directly or indirectly extends itself.<p>
<a name="78603"></a>
There is no analogue of the class <code>Object</code> for interfaces; that is, while every class is an extension of class <code>Object</code>, there is no single interface of which all interfaces are extensions.<p>
<a name="236095"></a>
The <i>superinterface </i>relationship is the transitive closure of the direct superinterface relationship. An interface <i>K</i><i></i> is a superinterface of interface <i>I</i><i></i> if either of the following is true:<p>
<ul><a name="236096"></a>
<li><i>K</i> is a direct superinterface of <i>I</i><i>.</i>
<a name="236097"></a>
<li>There exists an interface <i>J</i><i></i> such that <i>K</i><i></i> is a superinterface of <i>J</i><i></i>, and <i>J</i><i></i> is a superinterface of <i>I</i><i></i>, applying this definition recursively.
</ul><a name="236428"></a>
Interface <i>I</i><i></i> is said to be a <i>subinterface </i>of interface <i>K</i><i></i> whenever <i>K</i><i> </i>is a superinterface
of <i>I</i><i></i>.
<p><a name="236431"></a>
<h3>9.1.4 Interface Body and Member Declarations</h3>
<a name="78608"></a>
The body of an interface may declare members of the interface:
<p><ul><pre>
<i>InterfaceBody:<br>
</i><code> { </code><i>InterfaceMemberDeclarations</i><sub><i>opt</i></sub><code> }
</code>
<i>InterfaceMemberDeclarations:<br>
</i> <i>InterfaceMemberDeclaration<br>
</i> <i>InterfaceMemberDeclarations</i><code> </code><i>InterfaceMemberDeclaration
</i>
<i>InterfaceMemberDeclaration:<br>
</i> <i>ConstantDeclaration<br>
</i> <i>AbstractMethodDeclaration
</i></pre></ul><a name="17148"></a>
The scope of the name of a member declared in an interface type is the entire body
of the interface type declaration.
<p><a name="17144"></a>
<h3>9.1.5 Access to Interface Member Names</h3>
<a name="17145"></a>
All interface members are implicitly <code>public</code>. They are accessible outside the
package where the interface is declared if the interface is also declared <code>public</code>
and the package containing the interface is accessible as described in <a href="7.doc.html#26535">§7.1</a>.
<p><a name="32392"></a>
<h2>9.2 Interface Members</h2>
<a name="78624"></a>
The members of an interface are those members inherited from direct superinterfaces
and those members declared in the interface.
<p><a name="235920"></a>
The interface inherits, from the interfaces it extends, all members of those interfaces, except for fields that it hides and methods that it overrides.<p>
<a name="78642"></a>
<h2>9.3 Field (Constant) Declarations</h2>
<ul><pre>
<i>ConstantDeclaration:<br>
</i> <i>ConstantModifiers</i><code> </code><i>Type</i><code> </code><i>VariableDeclarator
</i>
<i>ConstantModifiers:</i> <i>one</i> <i>of<br>
</i> <code>public static final
</code></pre></ul><a name="40575"></a>
Every field declaration in the body of an interface is implicitly <code>public</code>, <code>static</code>, and <code>final</code>. It is permitted, but strongly discouraged as a matter of style, to redundantly specify any or all of these modifiers for such fields.<p>
<a name="40577"></a>
A constant declaration in an interface must not include any of the modifiers <code>synchronized</code>, <code>transient</code>, or <code>volatile</code>, or a compile-time error occurs.<p>
<a name="40725"></a>
It is possible for an interface to inherit more than one field with the same name <a href="8.doc.html#40491">(§8.3.3.3)</a>. Such a situation does not in itself cause a compile-time error. However, any attempt within the body of the interface to refer to either field by its simple name will result in a compile-time error, because such a reference is ambiguous.<p>
<a name="40729"></a>
There might be several paths by which the same field declaration might be inherited from an interface. In such a situation, the field is considered to be inherited only once, and it may be referred to by its simple name without ambiguity.<p>
<a name="40720"></a>
<h3>9.3.1 Initialization of Fields in Interfaces</h3>
<a name="236695"></a>
Every field in the body of an interface must have an initialization expression,
which need not be a constant expression. The variable initializer is evaluated and
the assignment performed exactly once, when the interface is initialized <a href="12.doc.html#44557">(§12.4)</a>.
<p><a name="236699"></a>
A compile-time error occurs if an initialization expression for an interface field contains a reference by simple name to the same field or to another field whose declaration occurs textually later in the same interface. Thus:<p>
<pre><a name="41008"></a>
interface Test {
<a name="41009"></a> float f = j;
<a name="41010"></a> int j = 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -