Java What is a difference between ? super E and ? extends E

public LinkedBlockingQueue(Collection<? extends E> c)

public int drainTo(Collection<? super E> c)

 The first says that it's "some type which is an subclass of E"; the second says that it's "some type which is a ancestor of E".

As an example, suppose you have a class hierarchy like this:

Parent extends Object
Child extends Parent

and a LinkedBlockingQueue<Parent>. You can construct this passing in a List<Child> which will copy all the elements safely, because every Child is a parent. You couldn't pass in a List<Object> because some elements might not be compatible with Parent.

Likewise you can drainTo that queue into a List<Object> because every Parent is an Object... but you couldn't drainTo it into a List<Child> because the List<Child> expects all its elements to be compatible with Child.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章