• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is <? extends> meaning ?

 
Soumya Padhiary
Greenhorn
Posts: 20
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Tutorials.
 
James X Peterson
Whizlabs Java Support
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell Ritchie,


helpfull information. Thanks for your link.

Regards,
James
 
Mack Wilmot
Ranch Hand
Posts: 88
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot add anything to a Something of <?>
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic