0
0
JavaConceptBeginner · 3 min read

Bounded Type Parameter in Java: What It Is and How It Works

A bounded type parameter in Java is a way to limit the types that can be used as arguments for a generic type. It uses the extends keyword to specify an upper bound, ensuring the type must be a subclass or implement a certain class or interface.
⚙️

How It Works

Imagine you have a box that can hold any kind of fruit, but you want to make sure it only holds apples or fruits similar to apples. In Java, a bounded type parameter works like that box. It restricts the generic type to a certain family of types.

When you declare a generic type with a bound, you tell Java: "Only accept types that are a subclass or implement a specific class or interface." This helps the program know what methods and properties are safe to use on the generic type, avoiding errors.

For example, if you say <T extends Number>, Java knows that T can only be a type like Integer, Double, or any class that inherits from Number. This way, you can use methods from Number safely on T.

💻

Example

This example shows a generic class that only accepts types extending Number. It calculates the double value of the number passed.

java
public class Box<T extends Number> {
    private T value;

    public Box(T value) {
        this.value = value;
    }

    public double doubleValue() {
        return value.doubleValue();
    }

    public static void main(String[] args) {
        Box<Integer> intBox = new Box<>(10);
        Box<Double> doubleBox = new Box<>(5.5);

        System.out.println("Integer box double value: " + intBox.doubleValue());
        System.out.println("Double box double value: " + doubleBox.doubleValue());

        // The following line would cause a compile error because String does not extend Number
        // Box<String> stringBox = new Box<>("Hello");
    }
}
Output
Integer box double value: 10.0 Double box double value: 5.5
🎯

When to Use

Use bounded type parameters when you want to create flexible and reusable code but still need to restrict the types to a certain group. This ensures safety and clarity.

For example, if you write a method that works with numbers, bounding the type to Number lets you use numeric methods without errors. It is also useful when working with collections where you want to accept only certain types.

In real life, think of it like a tool that only works with screws of a certain size. You want it to be flexible but not accept anything that won't fit.

Key Points

  • Bounded type parameters use extends to limit types.
  • They ensure type safety by restricting generic types.
  • They allow using methods of the bound type safely.
  • They improve code flexibility and reusability.

Key Takeaways

Bounded type parameters restrict generic types to subclasses or implementations of a specific class or interface.
They use the extends keyword to set an upper bound on the type parameter.
This restriction allows safe use of methods from the bound type on the generic parameter.
Bounded types improve code safety and flexibility by limiting acceptable types without losing generality.
Use bounded type parameters when you want generic code that works only with a certain family of types.