• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic class type declaration

 
Robert O'Leary
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from K&B MasterExam B, Q60.



which statements inserted at //insert code here will compile?

A. class Pie<T extends Food>{
B. class Pie<T extends Fruit>{
C. class Pie<T extends Apple>{
D. class Pie<T extends Pie>{
E. class Pie<T super Apple>{
F. class Pie <T>{

Answers ABF.

My question is why is class Pie<T super Apple>{ not allowed ?
 
Punit Singh
Ranch Hand
Posts: 952
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<T super Apple> is invalid construct, as it will have no restriction for assignment. You could have assigned any type of Object to <T super Apple>.
Think about what will be T here. As Object is super of Apple, and every class instance in java is an Object then it <T super Apple> would accept anything, if this construct was allowed.
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yet, class Pie<T> is perfectly acceptable ....?!?!
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Beckett wrote:Yet, class Pie<T> is perfectly acceptable ....?!?!


As <T> has restriction that is T or subtypes of T.
means if you pass <Fruit> than it has upper limit of Fruit.
Means Fruit or subclasses of Fruit is allowed.
 
Robert O'Leary
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Singh wrote:<T super Apple> is invalid construct, as it will have no restriction for assignment. You could have assigned any type of Object to <T super Apple>.
Think about what will be T here. As Object is super of Apple, and every class instance in java is an Object then it <T super Apple> would accept anything, if this construct was allowed.


So with the generic type here we're trying to restrict what types can be used in a class. If you use <T super Apple>, the type can be: Apple, Fruit, Food or Object. And the problem is specifying Object as a generic is pointless??
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert O'Leary wrote:
Punit Singh wrote:<T super Apple> is invalid construct, as it will have no restriction for assignment. You could have assigned any type of Object to <T super Apple>.
Think about what will be T here. As Object is super of Apple, and every class instance in java is an Object then it <T super Apple> would accept anything, if this construct was allowed.


So with the generic type here we're trying to restrict what types can be used in a class. If you use <T super Apple>, the type can be: Apple, Fruit, Food or Object. And the problem is specifying Object as a generic is pointless??


Yes you got it right. Object is at the top in the hierarchy, so it will not put any restriction.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic