Forums Register Login

Generics doubt

+Pie Number of slices to send: Send
Please have a look at the following class... The complier throws error in the mentioned line... but, i am not able to understand why

public class Wildcards {

public static void main(String[] args) {
List<Shape> data = new ArrayList<Shape>();
update(data);
}

public static void update(Collection<? extends Shape> values) {
values.add(new Shape()); //Compiler error
}

static class Shape {}

static class Circle extends Shape {}

static class Square extends Shape {}
}

The error is:
The method add(capture-of ? extends Wildcards.Shape) in the type Collection<capture-of ? extends Wildcards.Shape> is not applicable for the arguments (Wildcards.Shape).

Thanks,
Sudhakar R
+Pie Number of slices to send: Send
Method argument List<? extends Shape > is the reason for error.
this bounded wild card means we cant add anything but null.
can get() Shape only. and can pass List of Subclass of Shape also.
+Pie Number of slices to send: Send
"Sudhakar R", please check your private messages. You can see them by clicking My Profile.
Looky! I'm being abducted by space aliens! Me and this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 793 times.
Similar Threads
Generic type to raw type Or raw type to generic type?
Generics Casting
Generics
Restriction on parent class
Generics
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:21:03.