File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics Problem" Watch "Generics Problem" New topic

Generics Problem

AkhileshA KumarK

Joined: Jan 26, 2012
Posts: 1
hello everybody...
I am facing very problems in the Generics topic...below given is the question and the correct answers are options-a,b,c,e ...but i am not able to get it......
Please help me out by explaining that why these options are correct and why the rest ones are incorrect...

abstract class A<K extends Number>
// Insert here

which are the valid method declarations from the options given below..?

a. public abstract <K>K useMe(Object t);
b. public abstract <K> A<? extends Number> useMe(A<? super K>t);
c. public abstract <K> A<? super Number> useMe(A<? extends K>t);
d. public abstract <K> A<K> useMe(A<K>t);
e. public abstract <V extends K> A<V> useMe(A<V>t);
f. public abstract <V super K> A<V> useMe(A<V>t);
g. public abstract <V extends Character> A<? super V> useMe(A<K>t);
h. public abstract <V super Character> A<? super V> useMe(A<K>t);

Thanks in advance
Akhilesh Kumar
Ricardo Plascencia Silva

Joined: Dec 28, 2011
Posts: 18

AkhileshA KumarK wrote:

abstract class A<K extends Number>
// Insert here

Hi Akhilesh Kumar,

[ First at all, I would like to ask all the Java Gurus on this forum if you all can correct me if I'm wrong.. ) ]

In this abstract class declaration, you're using <K extends Number> as a placeholder for the type,.. with a range for the type that can be used, where you're saying that the type 'K' will be a Number or anything that extends Number (I know, this is old news).

a) public abstract <K>K useMe(Object t); -->

in this line/case, the type <K> is a new (definition) type, a redefinition type,.. so that's why we do not have any issue here.. (the new type <K> is hiding the type K already defined in the class declaration) this could apply for 'b' and 'c' also...

d) is not right because we have a bound mismatch, the type K is not a valid substitute for the bounded parameter <K extends Number> of the type A<K>

e) I do not seeing any issue here, with <V extends K> you're saying that the type 'V' will be a 'K' or anything that extends 'K'

f) this is wrong based on the e) explanation.

g) the "A<? super V>" type does not make sense.

h) here the "useMe" method has two return types? <V super Character> and A<? super V>

I agree. Here's the link:
subject: Generics Problem
jQuery in Action, 3rd edition