This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Murach's MySQL and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

generic return type

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear ranchers

Taken from Kathy Sierra & Bert Bates book's .

The following code :



gives the following compile error :

incompatible types
found : java.util.List<capture#774 of ? super Dog>
required: java.util.List<Dog>
output = process(input);

does anyone kindly tell me the reason for this error ?

By the way from what i read the book also stated that this is a valid code so i was wondering where did i go wrong

Thank you

Kevin
[ October 02, 2008: Message edited by: Kevin Liauw ]
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List<? super E>
This means "anything that is higher up the inheritance tree than E" -- In this code example, it is the type of the return value.

But we are passing a List <Integer>, which is indeed extending Number, but when compared to the rule List<? super E>, it fails because List<? super E> refers to something HIGHER THAN ANYTHING THAT EXTENDS NUMBER (In our case, its Integer).

So, we have to pass a type that is higher in the inheritance tree than Integer.

This is my understanding. Correct me if I am wrong.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what I think is happening-
You are passing an ArrayList<Integer> to the method, which is ok because your parameter should be a type that extends Number. But look at the return type requirement- List<? super e)- it should be either an Integer or a
super class of Integer. So the caller method should declare a reference variable wide enough to contain this collection in it. So, method 1 is -
In the caller method, declare, List<? super Integer> output=null
The other way is to continue having this line List<Integer> output=null,and do an appropriate cast- output=(List<Integer> process(output)

So the code will be


HTH,
Seema
 
Seema Gaurav
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Rekha,

it fails because List<? super E> refers to something HIGHER THAN ANYTHING THAT EXTENDS NUMBER



I might be wrong, but I think when you pass an ArrayList<Integer> to the method, all your <E>'s get replaced by <Integer> That means your return type, List<? super E> will be replaced by List<? super Integer>. There is nothing wrong in returning new ArrayList<Integer> but since I declared my return type as List<? super e>, there is a possiblity that this method returns an object which is an Integer or a superclass of Integer. So in the caller method, the variable that is going to hold this collection must be capable of accomodating an Integer or a super class of Integer, and I think that's why there is a compiler error. When I declare a variable large enough to hold the collection I'm returning, the compiler is happy. Or as an alternate, since I return an Integer, I can safely downcast it to a List<Integer>. Both ways my compiler doesn't crib.
This is what I think is happening, I might be wrong. So, please feel free to correct me.
Thanks,
Seema
[ October 02, 2008: Message edited by: Seema Gaurav ]
 
Seema Gaurav
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Kevin,
If this is the question you are referring to:
Chapter 7 Qn 16-
Given a method declared as:

This questions stands corrected as per KB Errata. They have mentioned that Line 1 should be replaced with:

620....bug.......Q-16: method declaration s/b:
& 634
public static <E extends Number> List<E> process(List<E> nums)

.You can check out their errata.

Having said that, I think my explanation above holds good for the declaration:

public static <E extends Number> List<? super E> process(List<E> nums)

Thanks,
Seema
 
Seema Gaurav
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rekha.
Just to reiterate, the point I was trying to drive home was that there is nothing wrong with the method declaration. I can still return a List of type: Integer, Number, or Object through that method. It will work fine as long as I assign it to a reference variable large enough to accomodate the collection returned by the method.
HTH,
Seema
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seema,
I think I got your point.
I should have told:

List<? super E> refers to something HIGHER THAN OR EQUAL TO ANYTHING THAT EXTENDS NUMBER



But what I am not clear is this --- Consider the difference in the 2 possible declarations of "output" variable:
1st possibility is List<Integer> output = null;
2nd possibility is List<? super Integer> output=null;

Comparing the above two possibilities, the parametrized type "Integer" satisfies the type "? super Integer". Then, why does the first one cause an issue, whereas the second one does not?
 
Seema Gaurav
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Rekha,
Let me make this very simple and try to explain it to you.
Consider :

In this method, although I'm returning a byte, I have declared my method to return an int,so Line 1 causes a compile time error as b is not large enough to accomodate the integer returned.

Something very similar is happening when I return List<? super E>. The method declares that it can return an object of type E or a super class of E. When I make such a declaration, the caller method should have a variable large enough to accomodate an Integer or any of its superclasses.List<Integer> cannot accomodate an onject of class Object or Number.

Just like how declaring byte b in the above code causes a compile time error, I'll get an error when I declare List<Integer> output=null; In the above code the workaround was to declare a larger datatype to accomodate the data being returned, we did that by saying int b=new A().callme(7); or by saying byte b=(byte) new A().callme(7);

Since I declared my method to return List<? super E>, I must accomodate for it by having a variable large enough or I must cast appropriately.


I hope that helps you understand what I mean. If it is still not clear, let me know. I will try to make it simpler for you.
Thanks,
Seema
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup. Absolutely clear Seema.

Nice comparative, on-time explanation

Thanks !!!
 
Seema Gaurav
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad it helped, Rekha
 
Kevin Liauw
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Rekha and Seema , i think i understand now
 
reply
    Bookmark Topic Watch Topic
  • New Topic