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

Contravariance with '? super T': Write-Only Semantics

Why super wildcards allow safe writing but return Object when reading elements.

Contravariance with '? super T': Write-Only Semantics

You'll learn why super wildcards are designed for writing elements safely, while reading returns only Object.

The Core Idea

When you declare a collection with <? super T>, you're saying "this collection holds objects of type T or any of its superclasses." This creates a write-friendly, read-unfriendly scenario.

Why you can write safely: You know that anything of type T (or its subtypes) can safely go into the collection. Why? Because T is a subtype of whatever unknown type the collection actually holds. If it's a List<? super Integer>, it might be List<Integer>, List<Number>, or even List<Object> — but an Integer fits in all of those.

Why reading is problematic: The compiler doesn't know the actual type stored in the collection. It could be Number, Object, or something else entirely. The only type the compiler can guarantee is Object, since that's the supertype of everything. So when you read from a <? super T> collection, you get Object back — not very useful.

The Analogy

Think of a donation bin labeled "accepts Size M shirts or larger." You can safely donate your Size M shirt (write operation). But if you reach in to grab something (read operation), you have no idea what size you'll get — could be M, L, XL, or even a bedsheet. You only know it's "some piece of fabric."

Write-Only in Practice

This design makes <? super T> perfect for consumers — methods that accept elements and store them, like addAll() operations. You're guaranteed type safety when adding, but you sacrifice precise type information when retrieving.

Key Takeaway:
<? super T> enables safe writing of T objects but forces reading as Object, making it ideal for consumer operations where you primarily insert rather than retrieve.