Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

extends — inheritance and upper bounds

extends — inheritance and upper bounds: extends names a class’s direct superclass, an interface’s direct superinterfaces, or the upper bound of a type parameter or wildcard.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

extends

extends names a class’s direct superclass, an interface’s direct superinterfaces, or the upper bound of a type parameter or wildcard.

A class extends at most one class, an interface may extend multiple interfaces, and ? extends T supports covariant reading of T values.

Inheritance does not inherit constructors or expose private members; deep hierarchies and using inheritance instead of composition increase coupling.

Omitting extends gives a class Object as superclass except for Object itself; in <T extends A & B>, a class bound must appear first.

Child inherits base from a class and two interface methods through Combined, so it prints base and the combined LR.

Examples

public class Main {
    static class Base {
        String base() { return "base"; }
    }
    interface Left { default String left() { return "L"; } }
    interface Right { default String right() { return "R"; } }
    interface Combined extends Left, Right {}
    static final class Child extends Base implements Combined {}

    public static void main(String[] args) {
        Child child = new Child();
        System.out.println(child.base());
        System.out.println(child.left() + child.right());
    }
}

Result

base
LR

Child inherits base from a class and two interface methods through Combined, so it prints base and the combined LR.

Sources