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

Doubt in Generics

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a doubt in K&B chapter7 16th question .

Given a method declared as:
public static <E extends Number> List<? super E> process(List<E> nums)
A programmer wants to use this method like this:
// INSERT DECLARATIONS HERE
output = process(input);
Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow
the code to compile? (Choose all that apply.)
A. ArrayList<Integer> input = null;
ArrayList<Integer> output = null;
B. ArrayList<Integer> input = null;
List<Integer> output = null;
C. ArrayList<Integer> input = null;
List<Number> output = null;
D. List<Number> input = null;
ArrayList<Integer> output = null;
E. List<Number> input = null;
List<Number> output = null;
F. List<Integer> input = null;
List<Integer> output = null;
G. None of the above.

For the above question B,E,F are given as correct.I tried to compile the code with B as option.But I am getting compiler error as Type mismatch:cannot convert from List<capture-of ? super Integer> to List<Integer>.

Is this question framed wrong??I have another doubt in this question even the option C should also be correct since the return type can be List<Integer> or anthinf which is super of Integer .So Number should also be correct..
I have one more doubt in this question

Do the co-variant returns not applicable to generics??Like can't we return an ArrayList to a method whose return type was declared as List.IF this is not true why we are passing an argument of type ArrayList to a method whose parameter was declared as of type List???


Can anyone please help me in this regard???
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just saw the question on page 16 and the method is:

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

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


Where did you get this one from.

For the first one C will be incorrect since the return type is List<Integer> which can't be applied to a List<Number>
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lalitha,

you are right this puzzled me as well when i was preparing. I am pretty sure this question is framed WRONG! But do try the things which compile successfully for various possibilities of input & output And please post them here!
 
lalitha kaparapu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for your reply..I have found this material in the cd which came along with K&B book.So if you substitute <List E> instead of <List ? super E> the correct answers will be B and F.
 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct but suppose we have our return type as List<? super E> itself ! Then it creates a hell of a confusion!! For example look at this:

---It WORKS!
But NOT the following:

---this DOESNT work
there are other many possibilities which look like they should work but they dont!! "jo" can be returned as only some "super of Integer" like Number, but not Integer! And you have to define "ouput" as some <? super> or else it wont work!!If you have some explanation, please let me know
 
lalitha kaparapu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nadeem,

Even I am also confused with this.I dont know why the compliler is giving error for this.
 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess we should direct our query directly to K & B then
 
Shiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic