Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B SJCP 5.0 Study Guide p. 634 - generics and collections question

 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 DECLARATION HERE
output = process(input);

Which pairs of declaration could be placed at //INSERT DECLARATION 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;

E. List<Integer> input = null;
List<Integer> output = null;

G. None of the above.


Try to figure out the answer...
.
.
.
.
.
.
.
Ok, according to the book correct answers are B,E and F.

My opinion is G. Code will not complie with any of given pairs. Why? Return type of the method is List<? super E>, so in my opinion regardless of what we really get from the method it cannot be assigned to any given in aswers references.


What we are getting from the method is a List of ANY type that is E or super of E. As we know polymorpshim applies only to the "base" type:

List<Dog> list = new ArrayList<Dog>(); // OK

Not the generic type:

ArrayList<Animal> list2 = new ArrayList<Dog>(); // WRONG

So, generic types must be the same both in reference declaration and when creating an object using new. In this case all references declarations given in answers are in my opinion wrong.



Let`s reffer to the correct answers:

B. ArrayList<Integer> input = null;
List<Integer> output = null;

Then what we can get as a return is a List that is a generic type Integer or ANY type that is super of Integer. So we can get a List that is a generic type Number as well. How it is possible then to assign it to List<Integer> reference if the generic types MUST be the same?

Next one:

E. List<Number> input = null;
List<Number> output = null;

We can get as a return is a List that is a generic type Number or ANY type that is super of Number. For example List<Object>, no way to assing it to List<Number>

E. List<Integer> input = null;
List<Integer> output = null;

Same reason as in B.


My opinion is that there are only two posibilities which are in fact the same:

List<?> output = null;
List<? extends Object> output = null;



Ofcourse I might be wrong and then would be very appreciated for claryfing the issue as my exam is coming closer Thank you.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This topic may help
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for link Barry. I was right Not that I`m happy that there`s a mistake in the question It just makes me feel better when thinking about passing the SCJP 5.0 exam.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic