Originally posted by Keith Nagle:
At compile time, you cannot add to a collection that has
<? extends ...> for the simple reason that you could be adding something that could upset your collection.
Just remember that if you want to add something to a collection, the following are legitimate:
Best regards.
[ July 11, 2008: Message edited by: Keith Nagle ]
I quickly tried this out and compiler was complaining:
$ javac GenericsPractice.java
GenericsPractice.java:25: cannot find symbol
symbol : method add(Animal)
location: interface java.util.List<capture of ? super Dog>
l.add(new Animal());
^
GenericsPractice.java:26: cannot find symbol
symbol : method add(java.lang.Object)
location: interface java.util.List<capture of ? super Dog>
l.add(new Object());
^
2 errors
I don't understand why? The code made perfect sense to me!
Any else have this bizarre problem??
It's as if the compiler cannot see "? super" ... this has got me worried now!
I'm using jdk 1.5.0_10
[ July 11, 2008: Message edited by: Frank Zito ]