File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic classes" Watch "Generic classes" New topic
Author

Generic classes

Ida Achi
Greenhorn

Joined: Oct 02, 2009
Posts: 26

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

Joined: Aug 03, 2008
Posts: 9321
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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 ... ...


BEE MBA PMP SCJP-6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic classes