And i understand all of it pretty well.
I'm just a bit confused about the following (orange part is most important if you want to skip a bit of reading):
What we really want is for the method to accept a list of [B]any[/B] kind of shape:[INDENT][B]public void[/B] drawAll(List<? [B]extends[/B] Shape> shapes) {
...
}
[/INDENT]There is a small but very important difference here: we have replaced the type List<Shape> with List<? [B]extends[/B] Shape>. Now drawAll() will accept lists of any subclass of Shape,
so we can now call it on a List<Circle> if we want. List<? [B]extends[/B] Shape> is an example of a [I]bounded wildcard[/I].
The ? stands for an unknown type, just like the wildcards we saw earlier. However, in this case, we know that this unknown type is in fact a subtype of Shape.
(Note: It could be Shape itself, or some subclass; it need not literally extend Shape.) We say that Shape is the [I]upper bound[/I] of the wildcard.
[COLOR=DarkOrange]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 shapes in the body of the method. For instance, this is not allowed:[/COLOR][INDENT][COLOR=DarkOrange][B]public void[/B] addRectangle(List<? [B]extends[/B] Shape> shapes) {
shapes.add(0, [B]new[/B] Rectangle()); // [I]Compile-time error![/I]
}
[/COLOR] [/INDENT][COLOR=DarkOrange]You should be able to figure out why the code above is disallowed. The type of the second parameter to shapes.add() is ? [B]extends[/B] 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. [/COLOR](Note that >>Rectangle extends Shape in the tutorial<<. )And especially this part:
> 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.Is there anyone who does understand what they mean there and cares to attempt to explain again or give a small example how that would turn wrong? ("if it might"):confused:
(I took code tags as i found them easier to read)


Sign In
Create Account


Back to top









