• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Confused by mixed generic type

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,

I have a small program like shown below :


Compilation failed at line 15 :

setElement(capture of ? extends A) in Basket<capture of ? extends A> cannot be applied to (A)

But if i change it to be

the compilation succeeded.
Someone explain it to me, please.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At compile time, you cannot add to a collection that has
<? extends ...> for the simple reason that you could be adding something that could upset your collection.
Just remember that if you want to add something to a collection, the following are legitimate:


Best regards.
[ July 11, 2008: Message edited by: Keith Nagle ]
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

At compile time, you cannot add to a collection that has <? extends ...>

Note: There is one exception: You can always add "null". For example the following will compile fine and print 1:
[ July 11, 2008: Message edited by: Ralph Jaus ]
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Faber

It seems that you have a lot of doubts in generics.
Please go through k&B Head First Java and SCJP 5 exam guide.

When you use
void show(SomeType<? extends SomeClassOrInterface> a)
you can not put any data in the object referenced by a .

But

When you use
void show(SomeType<? super SomeClassOrInterface> a)
you can put any object which is of type SomeClassOrInterface or its
sub ype in the object referenced by a.

I hope this will help you.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Nagle:
At compile time, you cannot add to a collection that has
<? extends ...> for the simple reason that you could be adding something that could upset your collection.
Just remember that if you want to add something to a collection, the following are legitimate:


Best regards.

[ July 11, 2008: Message edited by: Keith Nagle ]



I quickly tried this out and compiler was complaining:
$ javac GenericsPractice.java
GenericsPractice.java:25: cannot find symbol
symbol : method add(Animal)
location: interface java.util.List<capture of ? super Dog>
l.add(new Animal());
^
GenericsPractice.java:26: cannot find symbol
symbol : method add(java.lang.Object)
location: interface java.util.List<capture of ? super Dog>
l.add(new Object());
^
2 errors


I don't understand why? The code made perfect sense to me!
Any else have this bizarre problem??
It's as if the compiler cannot see "? super" ... this has got me worried now!
I'm using jdk 1.5.0_10
[ July 11, 2008: Message edited by: Frank Zito ]
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Frank Zito:


I quickly tried this out and compiler was complaining:
$ javac GenericsPractice.java
GenericsPractice.java:25: cannot find symbol
symbol : method add(Animal)
location: interface java.util.List<capture of ? super Dog>
l.add(new Animal());
^
GenericsPractice.java:26: cannot find symbol
symbol : method add(java.lang.Object)
location: interface java.util.List<capture of ? super Dog>
l.add(new Object());
^
2 errors


I don't understand why? The code made perfect sense to me!
Any else have this bizarre problem??
It's as if the compiler cannot see "? super" ... this has got me worried now!
I'm using jdk 1.5.0_10

[ July 11, 2008: Message edited by: Frank Zito ]



I'm with you. Made sense to me and I'm not understanding it now.

EDIT: I was making some tests and trying to figure out what's was happening and then a discovered.

When we use a List<? super Xxxx> as a parameter of a method the only type allowed to be add to the List is the Xxxxx. In the example where was used Dog we can ONLY ADD DOGS.
[ July 11, 2008: Message edited by: Raphael Rabadan ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hallo Raphael,

I think it is an inaccuracy in explanation. Suppose we have a method
<blockquote>code:
<pre name="code" class="java"> public void addItem(List<? super Dog> list) {
list.add(new Dog());
}
</pre>
</blockquote> and Dog extends Animal & Animal extends Object

This means that you can pass to the method lists of type Object, Animal and Dog and the new object of the class Dog can be added to passed lists.

You can not invoke list.add(new Animal()) because, for example, for the list of Dog the cast is needed.
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, Dennis.

You said what i've said :-)
He can only add Dog's in the example, you just give the explanation for it :-)

I didn't said I could pass only a List of Dogs :-)
 
Karl Prenton
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the code was a little misleading; probably the type of code to find in an exam!

obviously, code compiles for the more general case:
public void addAnother(List<? super Object> l)

which is equivalent as non-generic :
public void addAnother(List l)
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Frank Zito:
I guess the code was a little misleading; probably the type of code to find in an exam!

obviously, code compiles for the more general case:
public void addAnother(List<? super Object> l)

which is equivalent as non-generic :
public void addAnother(List l)



The equivalent to:

<blockquote>code:
<pre name="code" class="java">public void addAnother(List l)</pre>
</blockquote>

Wouldn't be:

<blockquote>code:
<pre name="code" class="java">public void addAnother(List<? extends Object> l)</pre>
</blockquote>

??

[ July 11, 2008: Message edited by: Raphael Rabadan ]
[ July 11, 2008: Message edited by: Raphael Rabadan ]
 
Denis Bogdanov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raphael Rabadan:

You said what i've said :-)
He can only add Dog's in the example, you just give the explanation for it :-)



Hallo Raphael,
You are right. This was an aim of my post. You are discovered WHATand i have tried to explain WHY
 
Karl Prenton
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
couple of useful links that may help explain my earlier statement:

http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html#Which%20types%20are%20permitted%20as%20wildcard%20bounds?

https://coderanch.com/t/385198/java/java/upper-bound-wildcard-readonly-but
 
Faber Siagian
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oooops, it is more confusing now.
Yes i know that we can't add any element into a collection, but in the code, nothing is added into basket. I just set basket's element.

Or there's something else that i missed about collection?
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Faber Siagian:
Oooops, it is more confusing now.
Yes i know that we can't add any element into a collection, but in the code, nothing is added into basket. I just set basket's element.

Or there's something else that i missed about collection?



You can't add, or change it. You could remove.

Let's think about it. Having a List<? super Dog> as a parameter of a method considering this hierarchy:


you could pass a List<Dog>, a List<Animal> and a List<Object>.
What would happen if you pass a List<Dog> considering this method:



Would it be possible? An Object for a List of Dogs?? Neither could be possible An Animal, because it could be a Cat, or anything else.

Thats why you can't use list.set(0,new Object()) too. Because you would be setting an Object to a list that could be of Dogs

I hope you understand now.

Raphael Rabadan
[ July 12, 2008: Message edited by: Raphael Rabadan ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic