aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic question from Master Exam 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 question from Master Exam" Watch "Generic question from Master Exam" New topic
Author

Generic question from Master Exam

Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

Hi Ranchers,

I have taken this question from Master Exam software.

class Food {}
class Fruit extends Food {}
class Apple extends Fruit {}
//insert code here

public static void main(String[] munch) {

Pie<Fruit> p = new Pie<Fruit>();

}
}

Given the answers which 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> {

The answers are A B and F


Can somebody why A B F correct and why not E is wrong ?
Especially I want to learn why E is wrong ?


blog: http://tunatore.wordpress.com
SCJP 6.0 + SCWCD 1.5
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Howdy Anut. Welcome to javaranch.

Well E is wrong because it is a syntax error. If you try to write a program which uses T super SomeClass, it will give you a compile time error. You can only use ? with super...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

Okay I got it
we can't use T in class but we are allowed to use ? with it!
Thanks
long meng
Ranch Hand

Joined: Oct 10, 2008
Posts: 58
I wrote
but it still doesn't work,why?


SCJP 5.0 98%<br />SCWCD 5.0 in progress . . .
long meng
Ranch Hand

Joined: Oct 10, 2008
Posts: 58
sorry,it's class Pie<? super Apple>
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

long are you sure. I think it should compile just fine...
long meng
Ranch Hand

Joined: Oct 10, 2008
Posts: 58
yeap,i wrote it in both eclipse and command line .In the eclipse,it told me "Syntax error on tokens, delete these tokens" on "public class GenericClass<? super Apple> {" sentence
Lukas Jenny
Greenhorn

Joined: Oct 12, 2008
Posts: 8
IMHO the ? can only be used with method parameters, not for classes
Graeme Jenkinson
Greenhorn

Joined: Sep 08, 2008
Posts: 27
I agree with Lukas, see the following:

http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#108850

It is somewhat terse and heavy going but it seems to support that the TypeBound can only be extends. Sierra-Bates (p627 Cert Guide 6) doesn't explicitly clarify things but the omission of any examples with the form <T super ...> also seems to support this intepretation.

thanks,

Graeme
[ October 13, 2008: Message edited by: Graeme Jenkinson ]
long meng
Ranch Hand

Joined: Oct 10, 2008
Posts: 58
so both "class Pie<T super Apple> {" and "class Pie<? super Apple> {" will cause a compile error,got it
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

Yeap I got it too....

? we cannot use it in class generic declaration
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic question from Master Exam