Bounded Type Parameters

Restricting type parameters

5 min read

Bounded Type Parameters

Bounds restrict the types that can be used as type arguments.

Code Examples

Upper bounded type parameter

java
1
2class Calculator<T extends Number> {
3    double sum(T a, T b) {
4        return a.doubleValue() + b.doubleValue();
5    }
6}
7          

Use Cases

  • Numeric computations
  • Type-safe constraints

Common Mistakes to Avoid

  • Using multiple class bounds
  • Incorrect bound order