Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics problem

 
neha verma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer to the 14 question below from K&B
import java.util.*;
public class LongWordFinder {
public static void main(String[] args) {
String[] array = { "123", "12345678", "1", "12", "1234567890"};
List<String> list = Arrays.asList(array);
Collection<String> resultList = getLongWords(list);
}
// INSERT DECLARATION HERE
{
Collection<E> longWords = new ArrayList<E>();
for (E word : coll)
if (word.length() > 6) longWords.add(word);
return longWords;
} }
Which declarations could be inserted at // INSERT DECLARATION HERE so that the program
will compile and run? (Choose all that apply.)
A. public static <E extends CharSequence> Collection<? extends CharSequence>
getLongWords(Collection<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)
Answer:F
I am not able to understand why answer A is not correct.
The return type from method getLongWords is Collection of type String
So as in option F we put a bounded option that is E extends CharSequence defining type E.
So why can't we use a wildcard extends CharSequence in the return type as in option A.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, ranchers,

Neha said:
I am not able to understand why answer A is not correct.
The return type from method getLongWords is Collection of type String
So as in option F we put a bounded option that is E extends CharSequence defining type E.
So why can't we use a wildcard extends CharSequence in the return type as in option A.


Because version A in itself IS correct.
But the line with the assignment
Collection<String> resultList = getLongWords(list);

would not compile.


The method is said to return a Collection<? extends CharSequence> and indeed returns a collection of strings when used as in the example question.
But the method may also return a List<StringBuffer> for example when invoked with a list of string buffers.
Therefore it cannot be allowed that a
Collection<String> = Collection<? extends CharSequence>
.

The method declaration is ok, and also matches to its body and return. The error comes from the wrong usage of the method.


You should compare this with the situation in answer F that does compile! Here the types of the invocation of the method and the assignment do match.

Yours,
Bu.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you compile the code with option A, notice that the syntax error is not in the return statement of the method, the syntax error is in the assignment of the return type of the method to Collection<String>.



The error is



The return type of the method is a Collection containing any class that implements CharSequence.

However, there are other classes besides String which implement CharSequence.

Consider the following example which doesn't compile.

 
neha verma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification. So in option F when we are putting a boundary limit on E that is

<

here we dont mean anything that extends CharSequence as the return type but this a condition that return type must satisfy. Please correct me if I am wrong.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Neha,

you are right.

Here's why this restriction to E extends CharSequence is needed.

Answer F:

The first <E extends CharSequence> is the declaration that the method is generic.
And it says what types the E can be.

The return type is Collection <E> and to make the story simple, the method parameter is of the same type.
So the Collections can be of type CharSequence (an interface type) or of its implementers.
e.g. String or StringBuffer ...
And it's wise to take this type, because inside the method there is the usage of method length() from variable word of this type (E). CharSequence has this method.

If the declaration would be just public static <E> Collec......
it could be called with any collection, but wouldn't compile, as Object for example doesn't have a method length();



Yours,
Bu.
[ July 17, 2007: Message edited by: Burkhard Hassel ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic