• 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

SCJP 5 Study Guide page 634

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a question about generics. The answer to question 16 says that two of the options can be dismissed because the method signature states that the return type is

List<? super E>

My confusion comes from the fact that, when using generics, base types can be treated polymorphically, but the generic types cannot. For example, we can declare

List<Animal> = new ArrayList<Animal>()

Can you please explain why we can't return an ArrayList<Integer> when List<Integer> is the declared return type?
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Can you please explain why we can't return an ArrayList<Integer> when List<Integer> is the declared return type?



You can return an ArrayList<Integer> when List<Integer> is the declared return type.

The question 16 (page 634) asked you to define the variable type for input/output value. But not the return object at "return" statement.
[ May 12, 2006: Message edited by: wise owen ]
 
Anderson Bush
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a typo in that reply? Did you mean to say "can't"? If not, I need more explanation. In a non-type-safe method, I can declare a return type of List then return a subtype of List. The answer to the self test question seems to imply that when using generics, if you declare that you return a base type of List, then you are restricting yourself to a List return exclusively. Is this correct? It seems maybe I am missing some fine point here...

Thank you.
 
Anderson Bush
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got more confusion on the same question. Let me detail the process I used to solve it, and perhaps you can see an error in my reasoning.

Let's consider option (C):
1. Method signature: public static <E extends Number> List<? super E>
process(List<E> nums)
2. ArrayList<Integer> input = null
3. The above is going to map to List<E> nums from the signature which makes E evaluate to Integer
4. Check: does E as Integer satisfy <E extends Number> ? Yes.
5. Now the only thing left to check is to make sure that the output is compatible with List<? super Integer>
6. List<Number> output = null. Does List<Number> satisfy List<? super Integer> ? Yes.
7. Conclude based on above reasoning that (C) is a valid option.

==> Please point out the flaw(s) in my reasoning.

Thank you.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you see the K&B errata (https://coderanch.com/t/253802/java-programmer-SCJP/certification/Updated-Errata), you will see that the question should be

620....bug.......Q-16: method declaration s/b:
& 634

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


Anyway when you have something looking strange you can always write code compile and run it to let the compiler and JVM tell you what happened

The way the question was written there would not be any right answer. Because if E is an Integer than output should be of type List<? super Integer> or List<?> or just List
 
Anderson Bush
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Allison - do you have any insight into my first question and Owen's response?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic