File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes What is <? extends> meaning ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is <? extends> meaning ?" Watch "What is <? extends> meaning ?" New topic
Author

What is <? extends> meaning ?

Soumya Padhiary
Greenhorn

Joined: Jan 10, 2013
Posts: 20

I think <? extends Object> means any class which is subtype of Object will be assign to the collection.
BUt in my programe:

[Added code tags - see UseCodeTags for details]

It shows compliation error:
cannot find symbol: add(int,Gen1)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
Java Tutorials.
James X Peterson
Whizlabs Java Support
Ranch Hand

Joined: Feb 26, 2013
Posts: 158
Hi Campbell Ritchie,


helpfull information. Thanks for your link.

Regards,
James
Mack Wilmot
Ranch Hand

Joined: Jul 27, 2011
Posts: 88

Soumya Padhiary wrote:

It shows compliation error:
cannot find symbol: add(int,Gen1)


That is all it shows? Are you sure? What JDK are you using?
Soumya Padhiary
Greenhorn

Joined: Jan 10, 2013
Posts: 20

Mack Wilmot wrote:
Soumya Padhiary wrote:

It shows compliation error:
cannot find symbol: add(int,Gen1)


That is all it shows? Are you sure? What JDK are you using?


I have executed it. I am using jdk 7 .
According to theory, its correct. But practically it is not happening .
Soumya Padhiary
Greenhorn

Joined: Jan 10, 2013
Posts: 20

Campbell Ritchie wrote:Java Tutorials.


Campbell,
Its not helping me.

Theoretically its correct.
But at the time of execution, i am getting compilation error again and again .
I am not able to find out the actual reason.

Help anybody.
Mack Wilmot
Ranch Hand

Joined: Jul 27, 2011
Posts: 88

Soumya Padhiary wrote:
Mack Wilmot wrote:
Soumya Padhiary wrote:

It shows compliation error:
cannot find symbol: add(int,Gen1)


That is all it shows? Are you sure? What JDK are you using?


I have executed it. I am using jdk 7 .
According to theory, its correct. But practically it is not happening .


Are you using an IDE? Here is the error I see trying to compile it on the command line and in NetBeans. What do you think the problem is?

Gen1.java:7: error: unexpected type
Vector<? extends Object> v1=new Vector<? extends Object>();
^
required: class or interface without bounds
found: ? extends Object
Gen1.java:8: error: no suitable method found for add(int,Gen1)
v1.add(1,new Gen1());
^
method Vector.add(int,CAP#1) is not applicable
(actual argument Gen1 cannot be converted to CAP#1 by method invocation co
nversion)
method Vector.add(CAP#1) is not applicable
(actual and formal argument lists differ in length)
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ? extends Object
2 errors



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
You cannot add anything to a Something of <?>
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4370
    
    8

If you want a list that can contain any subtype of Object, then what you need is simply a List<Object>.

A List<? extends Object> is something else. It's a variable that can reference any List<X>, where X is a specific type that is a subtype of Object. So all these are valid statements:


So...what objects do you think the compiler can safely allow you to add into all of those? You're trying to add a Gen1. That's fine if it's a List<Object> or a List<Gen1>, but what if it's one of the others? There's nothing that can be safely added to all of them*, so the compiler won't allow it.

(* More specifically, the only thing that can be safely be added to all of them is "nothing" - it will allow list.add(null))
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Also, this is invalid syntax:

On the right side of the = you cannot have a type with a wildcard. In other words, you cannot do new Vector<? extends Object>();, instead of ? extends Object you'll need to use a specific type, and not something with a wildcard.


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: What is <? extends> meaning ?