2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Difference between <? super T> and <? extends T> in Java

What is the difference between List<? super T> and List<? extends T> ?

I used to use List<? extends T>, but it does not allow me to add elements to it list.add(e), whereas the List<? super T> allows.

I want to know the difference.

userimage
This is nothing but setting the bounds

super is a lower bound, and extends is an upper bound.

According to http://download.oracle.com/javase/tutorial/extra/generics/morefun.html :

The solution is to use a form of bounded wildcard we haven't seen yet: wildcards with a lower bound. The syntax ? super T denotes an unknown type that is a supertype of T (or T itself; remember that the supertype relation is reflexive). It is the dual of the bounded wildcards we've been using, where we use ? extends T to denote an unknown type that is a subtype of T.

Answer is