📄 8.doc.html
字号:
</pre><a name="25369"></a>
result in a compile-time error: it would be impossible for any subclass of class
<code>Colored</code> to provide an implementation of a method named <code>setColor</code>, taking one
argument of type <code>int</code>, that can satisfy both <code>abstract</code> method specifications,
because the one in interface <code>Colorable</code> requires the same method to return no
value, while the one in class <code>Colored</code> requires the same method to return a value
of type <code>int</code> <a href="8.doc.html#40420">(§8.4)</a>.
<p><a name="36521"></a>
A class type should be declared <code>abstract</code> only if the intent is that subclasses can be created to complete the implementation. If the intent is simply to prevent instantiation of a class, the proper way to express this is to declare a constructor <a href="8.doc.html#16830">(§8.6.8)</a> of no arguments, make it <code>private</code>, never invoke it, and declare no other constructors. A class of this form usually contains class methods and variables. The class <code>java.lang.Math</code> is an example of a class that cannot be instantiated; its declaration looks like this:<p>
<pre><a name="36522"></a>
public final class Math {
<br><a name="36523"></a> private Math() { } // never instantiate this class
<br><br><a name="36538"></a> . . . declarations of class variables and methods . . .
<br><a name="36524"></a>}
</pre><a name="54727"></a>
<h4>8.1.2.2 final Classes</h4>
<a name="54729"></a>
A class can be declared <code>final</code> if its definition is complete and no subclasses are
desired or required. A compile-time error occurs if the name of a <code>final</code> class
appears in the <code>extends</code> clause <a href="8.doc.html#21723">(§8.1.3)</a> of another <code>class</code> declaration; this implies
that a <code>final</code> class cannot have any subclasses. A compile-time error occurs if a
class is declared both <code>final</code> and <code>abstract</code>, because the implementation of such a
class could never be completed <a href="8.doc.html#34944">(§8.1.2.1)</a>.
<p><a name="36559"></a>
Because a <code>final</code> class never has any subclasses, the methods of a <code>final</code> class are never overridden <a href="8.doc.html#227927">(§8.4.6.1)</a>.<p>
<a name="21723"></a>
<h3>8.1.3 Superclasses and Subclasses</h3>
<a name="29771"></a>
The optional <code>extends</code> clause in a class declaration specifies the <i>direct superclass</i>
of the current class. A class is said to be a <i>direct subclass</i> of the class it extends.
The direct superclass is the class from whose implementation the implementation
of the current class is derived. The <code>extends</code> clause must not appear in the definition
of the class <code>java.lang.Object</code> <a href="javalang.doc1.html#46442">(§20.1)</a>, because it is the primordial class
and has no direct superclass. If the class declaration for any other class has no
<code>extends</code> clause, then the class has the class <code>java.lang.Object</code> as its implicit
direct superclass.
<p><ul><pre>
<i>Super:<br>
</i> <code>extends </code><i>ClassType
</i></pre></ul><a name="229540"></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>ClassType:<br>
</i> <i>TypeName
</i></pre></ul><a name="29813"></a>
The <i>ClassType</i> must name an accessible <a href="6.doc.html#33916">(§6.6)</a> class type, or a compile-time error occurs. All classes in the current package are accessible. Classes in other packages are accessible if the host system permits access to the package <a href="7.doc.html#37758">(§7.2)</a> and the class is declared <code>public</code>. If the specified <i>ClassType </i>names a class that is <code>final</code> <a href="8.doc.html#54727">(§8.1.2.2)</a>, then a compile-time error occurs; <code>final</code> classes are not allowed to have subclasses.<p>
<a name="34088"></a>
In the example:<p>
<pre><br><a name="34089"></a>class Point { int x, y; }
<br><br><a name="34090"></a>final class ColoredPoint extends Point { int color; }
<br><a name="34094"></a>class Colored3DPoint extends ColoredPoint { int z; } // error
</pre><a name="29322"></a>
the relationships are as follows:
<p><ul><a name="29323"></a>
<li>The class <code>Point</code> is a direct subclass of <code>java.lang.Object</code><i></i>.
<a name="29324"></a>
<li>The class <code>java.lang.Object</code> is the direct superclass of the class <code>Point</code><i></i>.
<a name="35258"></a>
<li>The class <code>ColoredPoint</code> is a direct subclass of class <code>Point</code><i></i>.
<a name="35261"></a>
<li>The class <code>Point</code> is the direct superclass of class <code>ColoredPoint</code>.
</ul><a name="35273"></a>
The declaration of class <code>Colored3dPoint</code> causes a compile-time error because it
attempts to extend the <code>final</code> class <code>ColoredPoint</code>.
<p><a name="34079"></a>
The <i>subclass </i>relationship is the transitive closure of the direct subclass relationship. A class <i>A</i><i></i> is a subclass of class <i>C</i><i></i> if either of the following is true:<p>
<ul><a name="34080"></a>
<li><i>A</i> is the direct subclass of <i>C</i>.
<a name="34081"></a>
<li>There exists a class <i>B</i><i></i> such that <i>A</i><i></i> is a subclass of <i>B</i><i></i>, and <i>B</i><i></i> is a subclass of <i>C</i><i></i>, applying this definition recursively.
</ul><a name="34082"></a>
Class <i>C</i><i></i> is said to be a <i>superclass </i>of class <i>A</i><i></i> whenever <i>A</i><i> </i>is a subclass of <i>C</i><i></i>.
<p><a name="36594"></a>
In the example:<p>
<pre><br><a name="34023"></a>class Point { int x, y; }
<br><br><a name="34039"></a>class ColoredPoint extends Point { int color; }
<br><a name="34040"></a>final class Colored3dPoint extends ColoredPoint { int z; }
</pre><a name="35276"></a>
the relationships are as follows:
<p><ul><a name="36602"></a>
<li>The class <code>Point</code> is a superclass of class <code>ColoredPoint</code>.
<a name="36604"></a>
<li>The class <code>Point</code> is a superclass of class <code>Colored3dPoint</code><i></i>.
<a name="36611"></a>
<li>The class <code>ColoredPoint</code> is a subclass of class <code>Point</code>.
<a name="36613"></a>
<li>The class <code>ColoredPoint</code> is a superclass of class <code>Colored3dPoint</code><i></i>.
<a name="36619"></a>
<li>The class <code>Colored3dPoint</code> is a subclass of class <code>ColoredPoint</code>.
<a name="36621"></a>
<li>The class <code>Colored3dPoint</code> is a subclass of class <code>Point</code>.
</ul><a name="21739"></a>
A compile-time error occurs if a class is declared to be a subclass of itself. For
example:
<p><pre><br><a name="34029"></a>class Point extends ColoredPoint { int x, y; }
<br><a name="45856"></a>class ColoredPoint extends Point { int color; }
</pre><a name="25374"></a>
causes a compile-time error. If circularly declared classes are detected at run time,
as classes are loaded <a href="12.doc.html#44459">(§12.2)</a>, then a <code>ClassCircularityError</code> is thrown.
<p><a name="34031"></a>
<h3>8.1.4 Superinterfaces</h3>
<a name="18953"></a>
The optional <code>implements</code> clause in a class declaration lists the names of interfaces
that are <i>direct superinterfaces</i> of the class being declared:
<p><ul><pre>
<i>Interfaces:<br>
</i> <code>implements </code><i>InterfaceTypeList
</i>
<i>InterfaceTypeList:<br>
</i> <i>InterfaceType<br>
</i> <i>InterfaceTypeList</i><code> , </code><i>InterfaceType
</i></pre></ul><a name="229546"></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="34112"></a>
Each <i>InterfaceType</i> must name an accessible <a href="6.doc.html#33916">(§6.6)</a> 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
<a href="7.doc.html#13194">(§7.4.4)</a> and the interface is declared <code>public</code>.
<p><a name="36646"></a>
A compile-time error occurs if the same interface is mentioned two or more times in a single <code>implements</code> clause, even if the interface is named in different ways; for example, the code:<p>
<pre><a name="36651"></a>
class Redundant implements java.lang.Cloneable, Cloneable {
<a name="36652"></a> int x;
<a name="36654"></a>}
</pre><a name="36655"></a>
results in a compile-time error because the names <code>java.lang.Cloneable</code> and
<code>Cloneable</code> refer to the same interface.
<p><a name="31147"></a>
An interface type <i>I</i><i></i> is a <i>superinterface</i> of class type <i>C</i><i></i> if any of the following is true:<p>
<ul><a name="34118"></a>
<li><i>I</i><i></i> is a direct superinterface of <i>C</i>.
<a name="29862"></a>
<li><i>C</i><i></i> has some direct superinterface <i>J</i><i></i> for which <i>I</i><i></i> is a superinterface<i></i>, using the definition of "superinterface of an interface" given in <a href="9.doc.html#78598">§9.1.3</a>.
<a name="34121"></a>
<li><i>I</i><i></i> is a superinterface of the direct superclass of <i>C</i><i></i>, using this definition recursively.
</ul><a name="29918"></a>
A class is said to <i>implement</i> all its superinterfaces.
<p><a name="229105"></a>
In the example:<p>
<pre><a name="29875"></a>
public interface Colorable {
<a name="29876"></a> void setColor(int color);
<a name="29877"></a> int getColor();
<a name="29878"></a>}
<br><a name="29879"></a>
public interface Paintable extends Colorable {
<a name="29880"></a> int MATTE = 0, GLOSSY = 1;
<a name="29881"></a> void setFinish(int finish);
<a name="29882"></a> int getFinish();
<a name="29883"></a>}
<br><br><a name="29884"></a>class Point { int x, y; }
<br><a name="29885"></a>
class ColoredPoint extends Point implements Colorable {
<a name="29886"></a> int color;
<a name="29888"></a> public void setColor(int color) { this.color = color; }
<a name="29889"></a> public int getColor() { return color; }
<a name="29890"></a>}
<br><a name="29891"></a>
class PaintedPoint extends ColoredPoint implements Paintable <br>
{
<a name="29892"></a> int finish;
<a name="29893"></a> public void setFinish(int finish) {
<a name="230320"></a> this.finish = finish;
<a name="230321"></a> }
<a name="29894"></a> public int getFinish() { return finish; }
<a name="29895"></a>}
</pre><a name="29896"></a>
the relationships are as follows:
<p><ul><a name="29897"></a>
<li>The interface <code>Paintable</code> is a superinterface of class <code>PaintedPoint</code>.
<a name="29898"></a>
<li>The interface <code>Colorable</code> is a superinterface of class <code>ColoredPoint</code> and of class <code>PaintedPoint</code>.
<a name="29899"></a>
<li>The interface <code>Paintable</code> is a subinterface of the interface <code>Colorable</code>, and <code>Colorable</code> is a superinterface of <code>Paintable</code>, <code>a</code>s defined in <a href="9.doc.html#78598">§9.1.3</a>.
</ul><a name="29912"></a>
A class can have a superinterface in more than one way. In this example, the class
<code>PaintedPoint</code> has <code>Colorable</code> as a superinterface both because it is a superinterface
of <code>ColoredPoint</code> and because it is a superinterface of <code>Paintable</code>.
<p><a name="29870"></a>
Unless the class being declared is <code>abstract</code>, 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 <code>abstract</code> is not permitted to have <code>abstract</code> methods <a href="8.doc.html#34944">(§8.1.2.1)</a>.<p>
<a name="230327"></a>
Thus, the example:<p>
<pre><a name="16122"></a>
interface Colorable {
<a name="16123"></a> void setColor(int color);
<a name="16124"></a> int getColor();
<a name="16125"></a>}
<br><br><a name="16126"></a>class Point { int x, y; };
<br><a name="16127"></a>
class ColoredPoint extends Point implements Colorable {
<a name="16128"></a> int color;
<a name="16129"></a>}
</pre><a name="36705"></a>
causes a compile-time error, because <code>ColoredPoint</code> is not an <code>abstract</code> class but
it fails to provide an implementation of methods <code>setColor</code> and <code>getColor</code> of the
interface <code>Colorable</code>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -