extends
clause in a class declaration specifies the direct superclass
of the current class. A class is said to be a direct subclass of the class it extends.
The direct superclass is the class from whose implementation the implementation
of the current class is derived. The extends
clause must not appear in the definition of the class java.lang.Object
, because it is the primordial class
and has no direct superclass. If the class declaration for any other class has no
extends
clause, then the class has the class java.lang.Object
as its implicit
direct superclass.
Super:The following is repeated to make the presentation here clearer:
extends
ClassType
ClassType:The ClassType must name an accessible 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 and the class is declared
TypeName
public
. If the specified ClassType names a class that is final
, then a compile-time error occurs; final
classes are not allowed to have subclasses.
the relationships are as follows:
class Point { int x, y; }
final class ColoredPoint extends Point { int color; }
class Colored3DPoint extends ColoredPoint { int z; } // error
Point
is a direct subclass of java.lang.Object
.
java.lang.Object
is the direct superclass of the class Point
.
ColoredPoint
is a direct subclass of class Point
.
Point
is the direct superclass of class ColoredPoint
.
Colored3dPoint
causes a compile-time error because it
attempts to extend the final
class ColoredPoint
.
The subclass relationship is the transitive closure of the direct subclass relationship. A class A is a subclass of class C if either of the following is true:
the relationships are as follows:
class Point { int x, y; }
class ColoredPoint extends Point { int color; }
final class Colored3dPoint extends ColoredPoint { int z; }
Point
is a superclass of class ColoredPoint
.
Point
is a superclass of class Colored3dPoint
.
ColoredPoint
is a subclass of class Point
.
ColoredPoint
is a superclass of class Colored3dPoint
.
Colored3dPoint
is a subclass of class ColoredPoint
.
Colored3dPoint
is a subclass of class Point
.
causes a compile-time error. If circularly declared classes are detected at run time, as classes are loaded, then a
class Point extends ColoredPoint { int x, y; }
class ColoredPoint extends Point { int color; }
ClassCircularityError
is thrown.