Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic classes

 
Ida Achi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

From K and B SCJP6 master exams

The answer to following question are A, B and F.
I can understand A and B being correct but F?
F could put a car in the Fruit basket.
Why are T and E not considered correct? Please explain.



Given:



Which 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> {
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ida Achi wrote:I can understand A and B being correct but F?
F could put a car in the Fruit basket.

F is correct because if we declare class Pie with T, then it can be instantiated with any type, its not bounded.
Ida Achi wrote:Why are T and E not considered correct? Please explain.

Do you mean why C and E are not correct?? C is wrong because if we set Pie class as sub-type of Apple, then line 8 will not compile as Fruit is a superclass of Apple not a subclass. E is syntactically wrong, <T super Apple> is not the correct syntax as super can only be used with ? wildcard...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ida Achi wrote :

Why are T and E not considered correct? Please explain.


Ankit Garg wrote :

Do you mean why C and E are not correct??


I think, He, Ida Achi means T and E (the placeholders), which we declare to indicate the generic type in the class or parameter of method. And, Ida Achi, we can use any other Alphabetic character for that. Somebody confirm!

Thanks in Advanced!
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually none of them will compile because of the typo near
the end of line 8. And beyond this, answer A also has a typo,
but I know that's not the point . . .
Jim ... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic