Skip to content

Bounded Type Parameters

In the context of [[Java Generics]], Bounded Type Parameters are used to restrict the types that can be used as type arguments for a generic type or method^[600-developer__java__java-base__Generics.md]. This is achieved by specifying an upper bound for the type parameter, which limits the argument to be a subtype of a specific class or implementor of a specific interface^[600-developer__java__java-base__Generics.md].

Syntax and Usage

Bounded parameters are declared using the extends keyword followed by the bounding class or interface^[600-developer__java__java-base__Generics.md]. A common syntax is <U extends Number>, which restricts U to be Number or a subclass of Number^[600-developer__java__java-base__Generics.md].

In the following example, the method showKeyName accepts a Generic object, but the generic type U is constrained to extend the Number class^[600-developer__java__java-base__Generics.md].

class Generic<T> {
    T key;

    public T getKey() {
        return key;
    }

    public <U extends Number> U showKeyName(Generic<U> [container](<./container.md>)) {
        System.out.println("[container](<./container.md>) key :" + [container](<./container.md>).getKey());
        U test = [container](<./container.md>).getKey();
        return test;
    }
}

Sources

^[600-developer__java__java-base__Generics.md]