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

Generics method declaration confusion. Plz clarify

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one please clarify me why this method signature below is wrong -

public static <A super Apple> void insertRipe(A apple, Basket<? extends A> basket)

It is a method which inserts only ripe apples into the basket. Apple sub-classes exist. The following signature is correct -

public static <A extends Apple> void insertRipe(A apple, Basket<? super A> basket)

Basket is a simple generic class as follows -

public class Basket<E> {
private E element;

public void setElement(E x) {
element = x;
}

public E getElement() {
return element;
}
}

Thanks in advance!
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ahsan Habib:
Can any one please clarify me why this method signature below is wrong -

public static <A super Apple> void insertRipe(A apple, Basket<? extends A> basket)



You can't use A super Apple, for super type bound you have to use ?..

public static <? super Apple> void insertRipe(A apple, Basket<? extends A> basket)

But then you can't use your code as you want as you will have to modify the complete method to look like this

public static <? super Apple> void insertRipe(? apple, Basket<? extends ?> basket)

but this will give you compilation errors...
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by M. Piva:

This is as generic method declaration, you cant' use ?.
The correct syntax is:

public <T> void method(T t)



If you would have read my post completely, then you would have realized that in the end I said that it will not compile....
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@piva

why did you remove your posts??? Your second post was correct. It was the solution to the problem. post it back

also change your name. M. Piva is not a legal name here. So write your full name...
 
Marco Piva
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not being sure i prefered to delete it, anyway it was:

"The problem is that in the first code you're trying to modify a Basket object using the "? extends" wildcard.

When using either <? extends Class> or <?> wildcard, the collection can be accessed but NOT modified (compilation error).

When using a wildcard, List<? super Class>, the collection can be accessed AND modified."
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if the following example will make sense, but this is what i understood:
---------------------------------------------------
In case of <T extends String>, the compiler will replace all occurrences of T with 'String' (i.e. upper bound) with Type Erasure. And this makes perfect sense.

Had this been allowed (which is not) <T super String>, then the compiler has to replace all occurrences of T with Object; in which case even object of type Dog can be passed to T.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic