wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  EXAMLAB QUESTION - Can't understand the Answer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " EXAMLAB QUESTION - Can Watch " EXAMLAB QUESTION - Can New topic
Author

EXAMLAB QUESTION - Can't understand the Answer

Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183




Hi Everybody !!

I'm new to this forum. Currently preparing for SCJP 5.0.
I need some help in a Examlab Question. I could not understand the answer.


abstract class A<K extends Number>{

//INSERT HERE
}


Which are valid declarations for the above class ?? Choose all that apply.

A. abstract <K> K use(Object k);

B. abstract <K> A<? extends Number> use( A<? super K> y);

C. abstract <K> A<? super Number> use( A<? extends K> y) ;

D. abstract <K> A<K> use( A<K> k) ;

E. abstract <K extends Number> A<K> use1( A<K> k) ;

F. abstract <V extends K> A<V> use( A<V> k) ;

G. abstract <V super K> A<V> use( A<V> k) ;

H. abstract <V super Character> A<? extends V> use( A<V> k)

I. abstract <V extends Character> A<? super V> use( A<V> k) ;


The answer is - Options A , B , C, E and F are correct.

I CANNOT understand why options B & C are correct.

In option B the argument is A<? super K> which means it can also take A<Object> but class declaration of
A ia A<K extends Number>. So A<? super K> shouldn't be allowed in the argument.

Similarly How is A<? super Number> allowed in the type parameter in option C.



PLEASE HELP !!. I think I am in big trouble.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Hi Simran, welcome to javaranch.

This questions has been asked before here, here and here. Check those discussions. The trick is in the <K> part of the declarations of the methods.

abstract <K> A<? extends Number> use( A<? super K> y);
abstract <K> A<? super Number> use( A<? extends K> y) ;

You see that the bolded parts declare a new K which is not bound like the K declared at the class level, to clear things up, change the code like this

abstract <M> A<? extends Number> use( A<? super M> y);
abstract <M> A<? super Number> use( A<? extends M> y) ;


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EXAMLAB QUESTION - Can't understand the Answer
 
Similar Threads
Generics - method declarations -from Examlab
Java Generics
Generics Problem
Generic Declaration
Generic classes and Functions Problem