8.1.3 Superclasses and Subclasses

8.1.3 Superclasses and Subclasses

The optional 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(§20.1), 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:
extends ClassType

The following is repeated from §4.3 to make the presentation here clearer:

ClassType:

    TypeName

The ClassType must name an accessible (§6.6) 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 (§7.2) and the class is declared public. If the specified ClassType names a class that is final(§8.1.2.2), then a compile-time error occurs; final classes are not allowed to have subclasses.

In the example:

class Point { int x, y; }


final class ColoredPoint extends Point { int color; }

class Colored3DPoint extends ColoredPoint { int z; } // error

the relationships are as follows:

  • The class Point is a direct subclass of java.lang.Object**.
  • The class java.lang.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.

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 Ais a subclass of C.

In the example:

class Point { int x, y; }


class ColoredPoint extends Point { int color; }

final class Colored3dPoint extends ColoredPoint { int z; }

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 compile-time error occurs if a class is declared to be a subclass of itself. For example:

class Point extends ColoredPoint { int x, y; }

class ColoredPoint extends Point { int color; }

causes a compile-time error. If circularly declared classes are detected at run time, as classes are loaded (§12.2), then a ClassCircularityError is thrown.