• 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 question from Master Exam

 
Ranch Hand
Posts: 220
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
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
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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry,it's class Pie<? super Apple>
 
Ankit Garg
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
long are you sure. I think it should compile just fine...
 
long meng
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO the ? can only be used with method parameters, not for classes
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeap I got it too....

? we cannot use it in class generic declaration
 
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