Course contentsShow
← Java
Lesson 935 of 2,87022. Generics: Bounds and WildcardsFree lesson

Reading from <? super T> Collections Returns Object

Learn why reading from lower bounded wildcard collections only guarantees Object type and when this matters.

Reading from <? super T> Collections Returns Object

What you'll learn: You'll understand why retrieving elements from a lower bounded wildcard collection gives you only Object references, not the specific type you might expect.


The Core Problem

When you read from a collection declared with a lower bounded wildcard like List<? super Integer>, the compiler can only guarantee that what comes out is an Object. This is the opposite behavior from upper bounded wildcards, where you could read specific types but not write.

Why This Happens

A List<? super Integer> could be a List<Integer>, a List<Number>, or even a List<Object>. The compiler doesn't know which specific supertype it is at compile time. When you retrieve an element, the only safe type the compiler can guarantee is Object—the ultimate superclass of everything in Java.

The Practical Impact

List<? super Integer> list = new ArrayList<Number>();
list.add(42);           // Writing works fine
list.add(100);

Object obj = list.get(0);  // Must use Object
// Integer num = list.get(0);  // Compile error!

You must assign to Object, then manually cast if you need a more specific type:

Object obj = list.get(0);
if (obj instanceof Integer) {
    Integer num = (Integer) obj;
}

When This Matters

Lower bounded wildcards are designed for writing (producing values into the collection), not reading. If your code needs to read typed data from a collection, <? super T> is the wrong choice. Use <? extends T> for reading or an exact type parameter for both operations.

Key Takeaway: Reading from <? super T> collections returns Object because the compiler doesn't know the exact supertype; use lower bounds when you need to write, not read specific types.