Generic bounded type parameters in java
In Java, bounded type parameters are a powerful feature of generics that let you restrict the types that can be used as arguments for a type parameter.
They ensure type safety while still allowing flexibility and reuse.
🔹 What is a Bounded Type Parameter?
A bounded type parameter limits the types that can be passed to a generic class, interface, or method.
It is defined using the extends keyword (for both classes and interfaces).
🧩 Syntax
<T extends ClassName>
Here, T can only be a type that is either:
- The same as ClassName
- A subclass of ClassName
- A class implementing a specified interface
There are three ways to bound the type
- Upper Bound
- Lower Bound and
- Combined Bound
🔷 Upper Bounded Type (extends)
🧩 Definition:
Upper bounds restrict a generic type parameter to be a specific type or its subclass.
🧠 Syntax:
<T extends SuperClass>
Meaning —
Only SuperClass or its child classes can be passed.
🔹 Example: Upper Bound
class DataPrinter<T extends Number> {
private T data;
DataPrinter(T data) {
this.data= data;
}
void printData() {
System.out.println("Data: "+ data);
}
}
public class Main {
public static void main(String[] args) {
DataPrinter<Integer> dp1 = new DataPrinter<>(10);
DataPrinter<Double> dp2 = new DataPrinter<>(5.5);
DataPrinter<String> dp3 =new DataPrinter<>("My Data"); // Compile Time Error because upper bound of this class is Number
dp1.printData();
dp2.printData();
}
}
DataPrinter<Double> dp2 = new DataPrinter<>(5.5);
✅ Output:
Data: 10
Data: 5.5
🧠 Here:
T is bounded by Number.
So only subclasses of Number (like Integer, Double, Float, Long) are allowed.
🔷 Lower Bounded Type (super)
🧩 Definition:
A lower bound allows you to specify that a generic type must be a superclass of a specific type.
Used mainly with wildcards, not type parameters.
🧠 Syntax:
<? super Type>
This means — only Type or its superclasses are allowed.
✅ Example: Lower Bound (Wildcard)
import java.util.*;
public class Main {
public static void addNumbers(List<?superInteger> list) {
list.add(10);
list.add(20);
// list.add(10.5); ❌ Not allowed (Double not Integer or superclass)
}
public static void main(String[] args) {
List<Integer> intList = new ArrayList<>();
List<Number> numList = new ArrayList<>();
addNumbers(intList);
addNumbers(numList);
System.out.println(intList);
System.out.println(numList);
}
}
✅ Output:
[10, 20]
[10, 20]
Notes:
- <?super Integer> means: accept Integer or any superclass (Number, Object).
- We can add Integer, but not read as Integer (because actual type could be a superclass).
🔷 Combining Bounds (Advanced)
We can define more than one type parameter for a class or method.
<T extends ClassA & InterfaceB>
A type can extend one class but implement multiple interfaces.
Example:
class Printer<T extends Number & Comparable<T>> {
T value;
Printer(T value) {
this.value= value;
}
void show() {
System.out.println("Value: "+ value);
}
}
public class Main {
public static void main(String[] args) {
Printer<Integer> p = new Printer<>(10);
p.show();
// Printer<String> s = new Printer<>("Hello"); ❌ Not allowed
}
}
✅ Output:
Value: 10
🔹 What is a Wildcard?
A wildcard is written as a question mark (?), and it stands for “any type”.
🧠 Example:
List<?> list = new ArrayList<String>();
✅ Here:
List<?> means a list of unknown type.
It could be a list of String, Integer, Double, or any other object.
But since the exact type is unknown, you cannot add elements to it (except null). It means wildcard is only work for read only purpose not for writing purpose.
