File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EXAMLAB QUESTION - Can't understand the Answer

 
Simran Dass
Ranch Hand
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




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
Posts: 9496
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) ;
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic