Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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: 9692
42
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 ... ...
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic