So I was looking over some Java code and stumbled upon:
List extends SomeObject> l;
basically this list accepts all objects that ar
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
}
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
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.
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>()
As the first answer,
List<? extends SomeObject> l;
must contains Object that inherit from SomeObject, not some direct SomeObject.