Whats the use of saying <? extends SomeObject> instead of

后端 未结 5 1743
陌清茗
陌清茗 2020-11-27 18:54

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

List l;

basically this list accepts all objects that ar

相关标签:
5条回答
  • 2020-11-27 19:33
    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
            }
    
    0 讨论(0)
  • 2020-11-27 19:36

    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

    0 讨论(0)
  • 2020-11-27 19:40

    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.

    0 讨论(0)
  • 2020-11-27 19:43

    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>()

    0 讨论(0)
  • 2020-11-27 19:43

    As the first answer,

    List<? extends SomeObject> l;
    

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

    0 讨论(0)
提交回复
热议问题