This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
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 question from Master Exam

 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
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
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...
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I got it
we can't use T in class but we are allowed to use ? with it!
Thanks
 
long meng
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote
but it still doesn't work,why?
 
long meng
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry,it's class Pie<? super Apple>
 
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
long are you sure. I think it should compile just fine...
 
long meng
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO the ? can only be used with method parameters, not for classes
 
Graeme Jenkinson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so both "class Pie<T super Apple> {" and "class Pie<? super Apple> {" will cause a compile error,got it
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeap I got it too....

? we cannot use it in class generic declaration
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic