Whats the use of saying

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
        }

Leave a Comment