Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Problem

 
AkhileshA KumarK
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 18
Java Linux Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>


)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic