The optional extends clause in a normal class declaration specifies the direct superclass type of the class being declared.
ClassExtends:
extends ClassType
The extends clause must not appear in the definition of the class Object, or a compile-time error occurs, because it is the primordial class and has no direct superclass type.
The ClassType must name an accessible class (§6.6), or a compile-time error occurs.
It is a compile-time error if the ClassType names a class that is sealed (§8.1.1.2) and the class being declared is not a permitted direct subclass of the named class (§8.1.6).
It is a compile-time error if the ClassType names a class that is final, because final classes are not allowed to have subclasses (§8.1.1.2).
It is a compile-time error if the ClassType names the class Enum, which can only be extended by an enum class (§8.9), or names the class Record, which can only be extended by a record class (§8.10).
If the ClassType has type arguments, it must denote a well-formed parameterized type (§4.5), and none of the type arguments may be wildcard type arguments, or a compile-time error occurs.
The direct superclass type of a class whose declaration lacks an extends clause is as follows:
- The class
Objecthas no direct superclass type. - For a class other than
Objectwith a normal class declaration, the direct superclass type isObject. - For an enum class E, the direct superclass type is
Enum<E>. - For an anonymous class, the direct superclass type is defined in §15.9.5.
The direct superclass of a class is the class named by its direct superclass type. The direct superclass is important because its implementation is used to derive the implementation of the class being declared.
The superclass relationship is the transitive closure of the direct superclass relationship. A class A is a superclass of class C if either of the following is true:
- A is the direct superclass of C.
- Where a class B is the direct superclass of C, A is a superclass of B, applying this definition recursively.
A class is said to be a direct subclass of its direct superclass, and a subclass of each of its superclasses.
Example 8.1.4-1. Direct Superclasses and Subclasses
class Point { int x, y; }
final class ColoredPoint extends Point { int color; }
class Colored3DPoint extends ColoredPoint { int z; } // error
Here, the relationships are as follows:
- The class
Pointis a direct subclass ofObject. - The class
Objectis the direct superclass of the classPoint. - The class
ColoredPointis a direct subclass of classPoint. - The class
Pointis the direct superclass of classColoredPoint.
The declaration of class Colored3dPoint causes a compile-time error because it attempts to extend the final class ColoredPoint.
Example 8.1.4-2. Superclasses and Subclasses
class Point { int x, y; }
class ColoredPoint extends Point { int color; }
final class Colored3dPoint extends ColoredPoint { int z; }
Here, the relationships are as follows:
- The class
Pointis a superclass of classColoredPoint. - The class
Pointis a superclass of classColored3dPoint. - The class
ColoredPointis a subclass of classPoint. - The class
ColoredPointis a superclass of classColored3dPoint. - The class
Colored3dPointis a subclass of classColoredPoint. - The class
Colored3dPointis a subclass of classPoint.
A class C directly depends on a class or interface A if A is mentioned in the extends or implements clause of C either as a superclass or superinterface, or as a qualifier in the fully qualified form of a superclass or superinterface name.
A class C depends on a class or interface A if any of the following is true:
- C directly depends on A.
- C directly depends on an interface I that depends (§9.1.3) on A.
- C directly depends on a class B that depends on A, applying this definition recursively.
It is a compile-time error if a class depends on itself.
If circularly declared classes are detected at run time, as classes are loaded, then a ClassCircularityError is thrown (§12.2.1).
Example 8.1.4-3. Class Depends on Itself
class Point extends ColoredPoint { int x, y; }
class ColoredPoint extends Point { int color; }
This program causes a compile-time error because class Point depends on itself.