java is best because i can even drink it.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
java is best because i can even drink it.
addy sharma wrote:
1. if this myList is a list that can refer to any ArrayList which extends Object class and according to my code i am assigning the arraylist of string to this reference.then why i can not store the value as string object to it?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
List<? extends Object> myList= new ArrayList<String>();
myList = new ArrayList<FooBar>();
myList.add("ASDFG");
java is best because i can even drink it.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
addy sharma wrote:what is the scope of using this kind of collection objects?
java is best because i can even drink it.
Jesper de Jong wrote:The same with List<? super bigcats>. The compiler doesn't know what the exact type is of the elements in the list - it only knows that it's something that is a superclass of bigcats. You can't add anything to that list, for exactly the same reason as above.
java is best because i can even drink it.
Kindly,
Marouane Trabelsi.
R. Jain wrote:I'm afraid Jesper, but I've to disagree with you on this. You can add BigCats or any of it's subtypes to such lists. Right?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
R. Jain wrote:I'm afraid Jesper, but I've to disagree with you on this. You can add BigCats or any of it's subtypes to such lists. Right?
Wrong.
And, as he says, since the compiler doesn't know what that type is; it can't allow you to add anything.
Winston
R. Jain wrote:That is true for the Collection<? extends T>. But for Collection<? super T>, I can add an instance of subtype of T to it right?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Don't get me started about those stupid light bulbs. |