• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Generics doubt

Posts: 4
  • Mark post as helpful
  • send pies
  • 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>();

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).

Sudhakar R
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • 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
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Sudhakar R", please check your private messages. You can see them by clicking My Profile.
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic