• 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

What is <? extends> meaning ?

 
Greenhorn
Posts: 20
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Tutorials.
 
Whizlabs Java Support
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell Ritchie,


helpfull information. Thanks for your link.

Regards,
James
 
Ranch Hand
Posts: 88
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot add anything to a Something of <?>
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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))
 
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
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic