JVM Knowledge

The optional extends clause in a normal class declaration specifies the direct superclass of the current class.

Superclass:
  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.

The ClassType must name an accessible class type (§6.6), or a compile-time error occurs.

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 or any invocation of Enum (§8.9).

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.

Given a (possibly generic) class declaration C<F_1,...,F_n> (n ≥ 0, C ≠ Object), the direct superclass of the class type C<F_1,...,F_n> is the type given in the extends clause of the declaration of C if an extends clause is present, or Object otherwise.

Given a generic class declaration C<F_1,...,F_n> (n > 0), the direct superclass of the parameterized class type C<T_1,...,T_n>, where T_i (1 ≤ i ≤ n) is a type, is D<U_1 θ,...,U_k θ>, where D<U_1,...,U_k> is the direct superclass of C<F_1,...,F_n> and θ is the substitution [F_1:=T_1,...,F_n:=T_n].

A class is said to be a direct subclass of its direct superclass. The direct superclass is the class from whose implementation the implementation of the current class is derived.

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:

  • A is the direct subclass of C
  • There exists a class B such that A is a subclass of B, and B is a subclass of C, applying this definition recursively.

Class C is said to be a superclass of class A whenever A is a subclass of C.

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 Point is a direct subclass of Object.
  • The class Object is the direct superclass of the class Point.
  • The class ColoredPoint is a direct subclass of class Point.
  • The class Point is the direct superclass of class ColoredPoint.

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 Point is a superclass of class ColoredPoint.
  • The class Point is a superclass of class Colored3dPoint.
  • The class ColoredPoint is a subclass of class Point.
  • The class ColoredPoint is a superclass of class Colored3dPoint.
  • The class Colored3dPoint is a subclass of class ColoredPoint.
  • The class Colored3dPoint is a subclass of class Point.

A class C directly depends on a type T if T 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 reference type T if any of the following is true:

  • C directly depends on T.
  • C directly depends on an interface I that depends (§9.1.3) on T.
  • C directly depends on a class D that depends on T (using 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.