David Spades wrote:1. in this declaration, what should I put in the arraylist's bracket? any type that's a subclass or A or does it have to be A and only A?
2. in this upper bound wildcard, I can't add any item to the collection, even when it's of type A. why is this? what scenario is this constraint trying to prevent?
3. if I use ? super A, then list can be populated only with item of type A or its subclasses (rather contradictive with the "super" bound). why is this?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
David Spades wrote:my question is, when extracting the items out of listA, how does the compiler know what methods available for the extracted item?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:Yes. Remember that if you use <? extends Foo> you can take things out but not put things in.
Forget about casts. The compiler knows that everything which comes out of a Collection<? extends Foo> is a Foo, and that is enough to be getting on with.
Not sure about the upper bound question.
David Spades wrote:I tried it and it didn't give any warning or compile error. It however, gave me a runtime error.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Yes. No 1.David Spades wrote: . . . that's yes for which question? number 1? . . .
David Spades wrote:no warning, let alone compile error. but, I did get a big fat runtime error, as expected.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
David Spades wrote:1. It doesn't matter what type you choose for "right type" as long as when the wildcard on the "left expression" is substituted with "right type", the expression will pass, then it's all good
2. It has no effect whatsoever which "right type" you choose, there's not gonna be any effect at all. Is this true?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|