Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics doubt" Watch "Generics doubt" New topic
Author

Generics doubt

Sudhakar Rayavaram
Greenhorn

Joined: Dec 11, 2004
Posts: 4
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


-Sudhakar R
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

"Sudhakar R", please check your private messages. You can see them by clicking My Profile.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics doubt
 
Similar Threads
Generics
Generic methods and wildcards
Generics
Generics "is not applicable for the arguments"
Inferring type in call to generic method: how does it work?