• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

EXAMLAB QUESTION - Can't understand the Answer

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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) ;
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic