I have the following class:
interface Able{/* ... */}
class A implements Able{/* ... */}
and I have
Map
From http://download.oracle.com/javase/tutorial/extra/generics/wildcards.html:
There is, as usual, a price to be paid for the flexibility of using wildcards. That price is that it is now illegal to write into [a wildcard-based container]. For instance, this is not allowed:
public void addRectangle(List extends Shape> shapes) { shapes.add(0, new Rectangle()); // Compile-time error! }
You should be able to figure out why the code above is disallowed. The type of the second parameter to
shapes.add()
is? extends Shape
-- an unknown subtype ofShape
. Since we don't know what type it is, we don't know if it is a supertype ofRectangle
; it might or might not be such a supertype, so it isn't safe to pass aRectangle
there.