Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Generic type parameter bounds

Bounds restrict which types can be used as generic arguments. Learn how to preserve a builder’s concrete type, compare values through a supertype, and require several bounds at once.

Version: Java SE 21

Syntax

<T extends Builder<T>>
<T extends Comparable<? super T>>
<T extends Comparable<T> & Serializable>
<T extends Entity & Prioritized>

In Java SE 21, <T extends ...> declares an upper bound on a type parameter. Use extends for both class and interface bounds. The compiler checks that the type argument satisfies the bound and lets you use the accessible members it provides.

T extends Builder<T> is a recursive bound: T appears as a type argument in its own bound. When TaskBuilder extends Builder<TaskBuilder>, the inherited named method returns TaskBuilder, so you can call build next. The bound alone does not prove that this has type T; an abstract self() lets the subclass return this without a cast.

T extends Comparable<? super T> lets you pass a T to compareTo because the comparison accepts T or a supertype of T. Comparable is invariant: through ChronoLocalDate, LocalDate implements Comparable<ChronoLocalDate>, not Comparable<LocalDate>. So LocalDate satisfies the bound with the lower-bounded wildcard ? super T, but not Comparable<T> with T = LocalDate.

T extends Comparable<T> & Serializable requires both Comparable<T> and the marker interface Serializable. The bound does not perform serialization or guarantee that the whole object graph can be serialized; LocalDate is rejected despite implementing Serializable. In contrast, T extends Entity & Prioritized requires T to be Entity or a subclass and to implement Prioritized. In both declarations, & denotes a type intersection, not bitwise AND. If a class appears in a multiple bound, it goes first; only interfaces can follow. The leftmost bound determines the erasure of T.

Examples

import java.io.Serializable;
import java.time.LocalDate;

public class GenericBoundsDemo {
    abstract static class Builder<T extends Builder<T>> {
        protected String name;

        final T named(String name) {
            this.name = name;
            return self();
        }

        protected abstract T self();
    }

    static final class TaskBuilder extends Builder<TaskBuilder> {
        @Override
        protected TaskBuilder self() { return this; }

        Task build(int priority) { return new Task(name, priority); }
    }

    static class Entity {
        private final String name;

        Entity(String name) { this.name = name; }
        final String name() { return name; }
    }

    interface Prioritized {
        int priority();
    }

    static final class Task extends Entity implements Prioritized {
        private final int priority;

        Task(String name, int priority) {
            super(name);
            this.priority = priority;
        }

        @Override
        public int priority() { return priority; }
    }

    static <T extends Entity & Prioritized> String describe(T value) {
        return value.name() + " (priority " + value.priority() + ")";
    }

    static <T extends Comparable<? super T>> T min(T a, T b) {
        return a.compareTo(b) <= 0 ? a : b;
    }

    static <T extends Comparable<? super T>> T max(T a, T b) {
        return a.compareTo(b) >= 0 ? a : b;
    }

    static <T extends Comparable<? super T>> boolean isBefore(T a, T b) {
        return a.compareTo(b) < 0;
    }

    static <T extends Comparable<T> & Serializable> T minSerializable(T a, T b) {
        return min(a, b);
    }

    public static void main(String[] args) {
        Task task = new TaskBuilder().named("Backup").build(3);
        System.out.println(describe(task));

        LocalDate a = LocalDate.of(2026, 2, 1);
        LocalDate b = LocalDate.of(2026, 1, 15);
        LocalDate earlier = min(a, b);
        LocalDate later = max(a, b);
        System.out.println(earlier + " < " + later + ": " + isBefore(earlier, later));

        System.out.println(minSerializable("beta", "alpha"));
    }
}

Result

Backup (priority 3)
2026-01-15 < 2026-02-01: true
alpha

`named` preserves the `TaskBuilder` type, and `describe` uses both bounds. `min`, `max`, and `isBefore` accept `LocalDate` values; `minSerializable` compares strings without performing serialization.

Related lessons

  • Java · Lesson 11 · Advanced · Version: 2
  • Java · Lesson 15 · Advanced · Version: 2
  • Java · Lesson 16 · Advanced · Version: 2
  • Java · Lesson 32 · Intermediate · Version: 2

Sources