• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics doubt

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Sudhakar R", please check your private messages. You can see them by clicking My Profile.
reply
    Bookmark Topic Watch Topic
  • New Topic