Introduction
Java Generics make code more flexible and reusable by allowing classes, interfaces, and methods to work with different data types. However, there are situations where accepting any type is not appropriate. For example, if you are creating a method to calculate the square of a number or find the maximum value in a list, allowing types such as String or Boolean would not make sense.
To solve this problem, Java provides Bounded Types. Bounded Types allow developers to restrict the types that can be used as generic type arguments. Instead of accepting every object type, a generic class or method can specify that the type must extend a particular class or implement a specific interface.
Bounded Types were introduced with Java Generics in Java 5 and are widely used in the Java Collections Framework, utility classes, enterprise frameworks, and custom libraries.
What are Java Bounded Types?
Bounded Types allow you to restrict the types that can be used as generic type arguments.
Instead of allowing every object type, you specify that the type parameter must inherit from a particular class or implement a particular interface.
For example:
class Calculator {
}
Here:
- Integer is allowed.
- Double is allowed.
- Float is allowed.
- Long is allowed.
- String is not allowed.
This restriction makes the generic class safer and more meaningful.
Why Use Bounded Types?
Bounded Types provide several advantages:
- Restrict invalid data types
- Improve compile-time type safety
- Enable access to methods of the bounded class
- Simplify generic algorithms
- Improve API readability
- Prevent runtime errors
- Make reusable components more reliable
Without bounds, generic code may accept inappropriate types that do not support the required operations.
Syntax of Bounded Types
The basic syntax uses the extends keyword.
class ClassName
<T extends ParentClass> {
}
For interfaces:
class ClassName
<T extends InterfaceName> {
}
Although the keyword is extends, it is used for both classes and interfaces.
Bounded Generic Class
A generic class can restrict its type parameter.
Example
class Calculator<T extends Number> {
private T number;
Calculator(T number) {
this.number = number;
}
public double square() {
return number.doubleValue()
* number.doubleValue();
}
}
Usage:
Calculator<Integer> c1 =
new Calculator<>(10);
Calculator<Double> c2 =
new Calculator<>(5.5);
System.out.println(c1.square());
System.out.println(c2.square());
Output
30.25
Invalid Type Example
The following code causes a compilation error.
Calculator<String> c =
new Calculator<>("Java");
Reason
String does not extend Number, so it cannot satisfy the type bound.
The compiler catches this error before the program runs.
Bounded Generic Method
Methods can also define bounded type parameters.
Example
public class Main {
public static
<T extends Number>
double cube(T number) {
return Math.pow(
number.doubleValue(),
3
);
}
public static void main(String[] args) {
System.out.println(
cube(4)
);
System.out.println(
cube(2.5)
);
}
}
Output
15.625
The method accepts only numeric types.
Accessing Methods of the Bounded Class
One major benefit of Bounded Types is that the compiler knows the available methods.
Example
class Demo<T extends Number> {
T value;
Demo(T value) {
this.value = value;
}
public void print() {
System.out.println(
value.doubleValue()
);
}
}
Since T extends Number, methods such as:
- intValue()
- doubleValue()
- floatValue()
- longValue()
can be called safely.
Multiple Bounds
A type parameter can have multiple bounds.
Syntax:
<T extends ClassName
& Interface1
& Interface2>
Rules:
- Only one class can be specified.
- The class must appear first.
- Multiple interfaces can follow.
Example
class Example<
T extends Number
& Comparable<T>> {
}
This ensures that T is both a Number and comparable.
Bounded Types in Collections
Bounded Types are commonly used with collections.
Example
public static
double total(
List<? extends Number> list
) {
double sum = 0;
for(Number n : list) {
sum += n.doubleValue();
}
return sum;
}
This method accepts:
- List<Integer>
- List<Double>
- List<Float>
but rejects:
- List<String>
Real-Life Example
Suppose an educational application calculates student percentages.
class Result<T extends Number> {
private T marks;
Result(T marks) {
this.marks = marks;
}
public double percentage() {
return marks.doubleValue();
}
}
public class Main {
public static void main(String[] args) {
Result<Integer> r1 =
new Result<>(85);
Result<Double> r2 =
new Result<>(91.5);
System.out.println(
r1.percentage()
);
System.out.println(
r2.percentage()
);
}
}
Output
91.5
The same class supports different numeric types while preventing invalid ones.
Bounded Types vs Wildcards
| Bounded Type Parameter | Bounded Wildcard |
|---|---|
| Uses |
Uses extends Type> |
| Declares a reusable generic type | Accepts an unknown subtype |
| Used in generic classes and methods | Commonly used in method parameters |
| Can refer to the same type multiple times | Represents an unknown matching type |
Use a bounded type parameter when you need a named type throughout the class or method. Use a bounded wildcard when the method only needs to accept compatible generic types.
Advantages of Bounded Types
- Restrict invalid generic types
- Improve compile-time type safety
- Enable access to superclass methods
- Simplify reusable algorithms
- Improve API design
- Reduce runtime errors
- Enhance code readability
- Support enterprise-level reusable components
Common Mistakes
1. Using Unbounded Types Unnecessarily
Incorrect:
class Demo<T> {
}
If the class only works with numbers, use:
class Demo<T extends Number> {
}
2. Forgetting the extends Keyword
Bounds are always declared using extends, even for interfaces.
Correct:
<T extends Comparable<T>>
3. Incorrect Order of Multiple Bounds
Incorrect:
<T extends Comparable<T>
& Number>
Correct:
<T extends Number
& Comparable<T>>
The class must always appear before interfaces.
4. Assuming Primitive Types Can Be Used
Incorrect:
Calculator<Int>
Generics only support object types.
Correct:
Calculator<Integer>
5. Ignoring Type Constraints
Passing incompatible types, such as String to a class bounded by Number, results in compilation errors.
Best Practices
- Use Bounded Types whenever generic logic depends on specific methods or behavior.
- Keep bounds as general as possible while still enforcing correctness.
- Use wrapper classes instead of primitive types.
- Prefer bounded type parameters over manual type checking.
- Use multiple bounds only when necessary.
- Document type restrictions clearly in public APIs.
- Combine Bounded Types with Generics to create reusable, type-safe components.
Conclusion
Java Bounded Types extend the power of Generics by allowing developers to restrict generic type parameters to specific classes or interfaces. This ensures that generic code accepts only meaningful types, improves compile-time safety, and enables access to methods defined in the bounded superclass or interface.
Whether you are creating numeric utility classes, reusable algorithms, collection-based APIs, or enterprise frameworks, Bounded Types help you write cleaner, safer, and more maintainable code. By understanding upper bounds, multiple bounds, and their relationship with wildcards, you can design robust generic components that follow modern Java best practices and perform reliably in real-world applications.