• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic classes

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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> {
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ... ...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic