Originally posted by L Yan
Is it a rule that if generic type definition has a ?, then no object can be added to the collection, but other operations still work?
Since we don�t know what the element type of c stands for, we cannot add objects to it. The add() method takes arguments of type E, the element type of the collection.
When the actual type parameter is ?, it stands for some unknown type. Any parameter we pass to add would have to be a subtype of this unknown type. Since we don�t know what type that is, we cannot pass anything in. The sole exception is null, which is a member of every type.
-------------------------------------------------------------------------
List <? extends Object> is an example of bounded wildcard. The ? stands
for an unknown type. However, in this case, we know that this unknown type is in fact a subtype of Object. We say that Object is the upper bound of the wildcard.
It seems that compiler will allow adding any object in this case as ny object is subclass of Object.
But its wrong. Still u can't add nything here except null.
Take this case...
The type of the second parameter to shapes.add() is ? extends Shape - an unknown subtype of Shape. Since we don�t know what type it is, we don�t know if it is a supertype of Rectangle; it might or might not be such a supertype, so it isn�t safe to pass a Rectangle there.
regards