• 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

Ultimate generics Q

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from K&B MasterExam C:

Given that String extends java.lang.CharSequence


A) public static <E extends CharSequence> Collection<? extends CharSequence>
getLongWords(Collections <E> coll)

(B) public static <E extends CharSequence> List<E> getLongWords(Collection<E> coll)
(C) public static Collection<E extends CharSequence> getLongWords(Collection<E> coll)
(D) public static List<CharSequence> getLongWords(Collection<CharSequence> coll)
(E) public static List<? extends CharSequence> getLongWords(Collection<? extends CharSequence> coll)
(F) static public <E extends CharSequence> Collection<E> getLongWords(Collection<E> coll)
(G) static public <E super CharSequence> Collection<E> getLongWords(Collection<E> coll)

Correct answer is F. I can see why it is, but I have a question about (A) and (G):

Q1) For the reference to the answer, K&B say that the return type in (A) is too vague. Is that is must be Collection<String>, no polymorphic wiggle-room??
Q2) For (G), I know that super can never be used in a generic type declaration as above, but why?

I dont know if helps anyone else when I was tryin to figure out this Q, I found it easier to answer it by writing down what is legal for each piece of the method declaration and then eliminate (A) - (G) accordingly:

1) Generic Type Declaration (after public static in each answer)
<E>
<E extends CharSequence>
<E extends String> //pointless but legal

2) Return type
Collection<E>

3) Method parameter
Collection<E> coll
List<E> coll

Why do ye think?

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robert O'Leary wrote:This is from K&B MasterExam C:

Given that String extends java.lang.CharSequence


A) public static <E extends CharSequence> Collection<? extends CharSequence>
getLongWords(Collections <E> coll)

(B) public static <E extends CharSequence> List<E> getLongWords(Collection<E> coll)
(C) public static Collection<E extends CharSequence> getLongWords(Collection<E> coll)
(D) public static List<CharSequence> getLongWords(Collection<CharSequence> coll)
(E) public static List<? extends CharSequence> getLongWords(Collection<? extends CharSequence> coll)
(F) static public <E extends CharSequence> Collection<E> getLongWords(Collection<E> coll)
(G) static public <E super CharSequence> Collection<E> getLongWords(Collection<E> coll)

Correct answer is F. I can see why it is, but I have a question about (A) and (G):

Q1) For the reference to the answer, K&B say that the return type in (A) is too vague. Is that is must be Collection<String>, no polymorphic wiggle-room??
Q2) For (G), I know that super can never be used in a generic type declaration as above, but why?

I dont know if helps anyone else when I was tryin to figure out this Q, I found it easier to answer it by writing down what is legal for each piece of the method declaration and then eliminate (A) - (G) accordingly:

1) Generic Type Declaration (after public static in each answer)
<E>
<E extends CharSequence>
<E extends String> //pointless but legal

2) Return type
Collection<E>

3) Method parameter
Collection<E> coll
List<E> coll

Why do ye think?


In A. you have:

public static <E extends CharSequence> Collection<? extends CharSequence>
getLongWords(Collections <E> coll)

When you call the method, you are calling it using an argument that is a List<String>. This means that E is resolved to String (no problem there.) The problem is that you are assigning the return of your method to a Collection<String>, and you are returning a collection of a type which extends CharSequence (but which might not be String.) That's why A is illegal.

The reason why you can't use <E super X> is because it is illegal. And the reason why it is illegal is because it is of no use, because it doesn't impose any upper bounds on the type, which is what you care about, because upper bounds are what the compiler uses when applying type erasure. If you have <E extends X>, then upon type erasure, the compiler will insert a cast to type X wherever a reference of the generic type E is used, because E Is-A X. But <E super X> doesn't have any use, so it is illegal.

I think you are in the right path about how to think about these questions (look at the generic method argument, try to resolve E from the point of call, look at the return type of the method, and see if it is compatible to the assignment of the return to references in the main code.
 
Robert O'Leary
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:
In A. you have:

public static <E extends CharSequence> Collection<? extends CharSequence>
getLongWords(Collections <E> coll)

When you call the method, you are calling it using an argument that is a List<String>. This means that E is resolved to String (no problem there.) The problem is that you are assigning the return of your method to a Collection<String>, and you are returning a collection of a type which extends CharSequence (but which might not be String.) That's why A is illegal.



Ya I see what youre saying, you dont want it open-ended. You dont want a return type of Collection<StringBuffer> and other types that are of CharSequence to be possible .

Ruben Soto wrote:

The reason why you can't use <E super X> is because it is illegal. And the reason why it is illegal is because it is of no use, because it doesn't impose any upper bounds on the type, which is what you care about, because upper bounds are what the compiler uses when applying type erasure. If you have <E extends X>, then upon type erasure, the compiler will insert a cast to type X wherever a reference of the generic type E is used, because E Is-A X. But <E super X> doesn't have any use, so it is illegal.



Having no upper bounds is pointless...and the compiler doesnt want messing around!!

Thanks again Ruben. When you going to sit the exam yourself?

I need to do some more mocks..I aiming to do it in < 2 weeks time, I'll wait till Im scoring better more consistently tho!
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Robert, 2 weeks sounds good. I have been doing some other things lately, but I will schedule my exam soon as well. I need to find some time to go to the testing center and spend at least a couple hours doing the exam. It will happen soon.

Good luck with your exam, by the way.
 
reply
    Bookmark Topic Watch Topic
  • New Topic