Lower Bounded Wildcards: <? super T> Syntax
What you'll learn: How to use <? super T> to declare collections that accept a type or any of its supertypes.
The Core Idea
While <? extends T> lets you read from a collection (accepting T or any subtype), <? super T> does the opposite—it lets you write to a collection that holds T or any of its superclasses.
The syntax <? super T> means "some unknown type that is T or a supertype of T." This includes T itself, any parent class up to Object, and any interfaces T implements.
Why Lower Bounds Matter
Think of it like a storage container with a minimum capacity requirement. If you have a List<? super Integer>, it might be a List<Integer>, List<Number>, or even List<Object>. The key insight: you can safely add an Integer to any of these because Integer is-a Number and is-an Object.
List<? super Integer> list = new ArrayList<Number>();
list.add(42); // ✓ Safe: Integer fits in Number container
list.add(Integer.valueOf(100)); // ✓ Also safe
// Would also work:
List<? super Integer> objectList = new ArrayList<Object>();
objectList.add(7); // ✓ Integer fits in Object container
The Pattern
Lower bounded wildcards follow the "PECS" principle (Producer Extends, Consumer Super):
- Use
<? super T>when the collection is a consumer (you're putting T items into it) - The wildcard accepts T or any ancestor type
Key Takeaway: <? super T> creates a lower bound, allowing you to write objects of type T into a collection whose actual type is T or any supertype of T—perfect for methods that need to add elements.