Whats the use of saying <? extends SomeObject>

2019-01-07 17:59发布

问题:

So I was looking over some Java code and stumbled upon:

List<? extends SomeObject> l;

basically this list accepts all objects that are some kind of SomeObject - SomeObject itself or its inheritors. But according to polymophism, it's inheritors can also be seens as SomeObject, so this would work as well:

List<SomeObject> l;

So why would someone use the first option when the second is clearly defined and virtually identical?

回答1:

List<SomeObject> l;

In this you cannot say List<SomeObject> l = new ArrayList<SubClassOfSomeObjectClass>;(not allowed) wheres for

List<? extends SomeObject> l;

you can say

List<? extends SomeObject> l = new ArrayList<SubClassOfSomeObject>;(allowed)

But note that in List<? extends SomeObject> l = new ArrayList<SubClassOfSomeObject>; you cannot add anything to your list l because ? represents unknown class (Except null of-course).

Update: For your question in the comment What could I possibly do with a list if I cannot add anything to it?

Now consider a case in which you have to write a function to print your list but mind you it must only accept a List having objects which are subclasses of your SomeObject. In this case as I stated above you cannot use

public void printList(List<SubClassOfSomeObjectClass> someList)

So what would you do? You would do something like

    public void printList(List<? extends SomeObject> someList) {
        for(SomeObject myObj : someList) {
             //process read operations on  myObj
        }


回答2:

The key link you want to read is http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html which explains Generic wildcard in detail.

The List<SomeObject> is not the same as List<? extends SomeObject>. Observe the following

List<Object> x = new ArrayList<Object>();
List<String> y = new ArrayList<String>();
// x = y; Will throw Compilation exception
List<? extends Object> z = y; //will pass compilation

You may want to observe that you can add say a String to both the x and the y list however it will be useful when you write say a library function (such as the printCollection in the example shown in the link) rather than accepting a Collection<Object> in which case a user cannot pass his list of strings that he has to your method, if you accept Collection<? extends Object> then the user can pass his Collection<Apple>, Collection<Orange> etc without having to explicitly create another list.



回答3:

List<? extends SomeObject> l; is not accepting new SomeObject() but List<SomeObject> l; does.

what is also not working: List<SomeObject> l = new ArrayList<SubTypeOfSomeObject>()

what is working: List<? extends SomeObject> l = new ArrayList<SubTypeOfSomeObject>()



回答4:

X cannot be added to List<Y> even if X can be converted to Y.

So,in your second case if List<X> l; was allowed to add subclass of X,that would break the basic principal of type safety



回答5:

As the first answer,

List<? extends SomeObject> l;

must contains Object that inherit from SomeObject, not some direct SomeObject.