my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic class type declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic class type declaration" Watch "Generic class type declaration" New topic
Author

Generic class type declaration

Robert O'Leary
Ranch Hand

Joined: Mar 24, 2009
Posts: 41
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 ?


SCJP6
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
<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.


SCJP 6
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
Yet, class Pie<T> is perfectly acceptable ....?!?!
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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

Joined: Mar 24, 2009
Posts: 41
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

Joined: Oct 16, 2008
Posts: 952
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic class type declaration
 
Similar Threads
Use of generic term super in class declaration
Why the answer is... MasterExam
Generic classes
Generics ques...pl help explain
Generic question from Master Exam