Skip to content
Veltoryx Technologies
ProLesson 6.3Intermediate2 min readUpdated 30 October 2026

Bounded type parameters

Limit what a type parameter can be with extends, call methods on it safely, and write a generic max method like the standard library does.

A plain T can be anything, so you can only call Object methods on it. Sometimes you need more: "any number", or "anything that can be compared". A bound restricts the type parameter and unlocks those methods.

01Upper bounds with extends

<T extends Number> means "T can be Number or any subclass": Integer, Double, Long…

UpperBound.java
import java.util.List;

public class UpperBound {
    static <T extends Number> double sum(List<T> values) {
        double total = 0;
        for (T v : values) {
            total += v.doubleValue();     // allowed: T is a Number
        }
        return total;
    }

    public static void main(String[] args) {
        System.out.println(sum(List.of(1, 2, 3)));         // Integers
        System.out.println(sum(List.of(1.5, 2.5)));        // Doubles
        // sum(List.of("a", "b"));                          // doesn't compile
    }
}
Expected output
6.0
4.0

In bounds, extends is used for both classes and interfaces.

Loading the rest of the lesson…