Search code examples
javagenericsscjp

Question concerning SCJP-6 exam


While preparing for the SCJP-6 exam I faced with a difficult issue. I can’t find answer by myself. Please, answer for the question and give short comments:

abstract class A<K extends Number> {
   // insert code here
}    
  1. public abstract <K> A<? extends Number> useMe(A<? super K> k);

  2. public abstract <K> A<? super Number> useMe(A<? extends K> k);

  3. public abstract <K> A<K> useMe(A<K> k);

  4. public abstract <V extends K> A<V> useMe(A<V> k);

  5. public abstract <V super K> A<V> useMe(A<V> k);

  6. public abstract <V extends Character> A<? super V> useMe(A<K> k);

  7. public abstract <V super Character> A<? super V> useMe(A<K> k);

Which method can be inserted in the placeholder above?

P.S. I tried to look on the specification. Those one was not helpful for me.


Solution

  • Answer 1,2,3: The generic type K shadowed class type K. In those methods K is just new generic type. Compiler tries to pass conditional evaluation <K extends Number>. Method 1,2 passes and 3 - fails.

    Answer 4 is completely correct.

    Answer 5,7 has syntax error.

    Answer 6 incorrect, because test <V extends Number> will fail.