extends keyword

Syntax

|-extends-ClassType-|

Description
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 that it extends. The direct superclass is the class from whose implementation the current class is derived. If the class declaration for any other class has no extends clause, then that class has the class java.lang.Object as its implicit direct superclass.

The ClassType must name an accessible class type, or a compilation 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 if the class is declared public. If the specified ClassType names a class that is final, then a compilation error occurs; final classes are not allowed to have subclasses.

The optional extends clause in an interface declaration specifies the direct superinterfaces of the current interface.  An interface is said to be a direct subinterface of the interface that it extends.  The interface being declared extends each of the other named interfaces and therefore inherits the methods and constants of each of the other named interfaces. Any class that implements the declared interface is also considered to implement all the interfaces that this interface extends and that are accessible to the class.

The InterfaceType in the extends clause of an interface declaration must name an accessible interface type; otherwise a compilation error occurs.The InterfaceType is a package name followed by an identifier. If an interface directly or indirectly extends itself, a compilation error occurs. There is no analogue of the class Object for interfaces; that is, while every class is an extension of class Object, there is no single interface of which all interfaces are extensions.

Example

class Point { int x, y; }
final class ColoredPoint extends Point { int color; }
class Colored3DPoint extends ColoredPoint { int z; } // error

In the preceding example, the relationships are as follows:

The declaration of class Colored3dPoint causes a compilation error because it attempts to extend the final class ColoredPoint.

Here is another example.

interface BaseInterface;
interface OtherInterface;
interface DerivedInterface extends BaseInterface;
interface OtherDerivedInterface extends BaseInterface, OtherInterface;

In the second example, the relationships are as follows:

ngrelr.gif (548 bytes)
Syntax diagrams
class keyword
implements keyword
interface keyword

Source: The Java Language Specification. Copyright (C) 1996 Sun Microsystems, Inc.