• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Generics question from bonus exam

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;

class Apple{}
class Macintosh extends Apple{
public static void main(String[] args){
List<Apple> apples = new ArrayList<Apple>();
basket(a);
}
//insert code here
}


Which inserted at //insert code here will compile?



why can't the following be inserted?
static void basket(List<? super Apple> list){list.add(new Object());}
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sital you cannot add anything which is not the subclass of Apple in the following case and Apple itself that's why it give a compiler error when you try to add a Object in the list.
 
Sital Kotamraju
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I am clear on this now. Please correct me if I am wrong.

import java.util.*;

class Fruit{}

class Apple extends Fruit{}

class Gala extends Apple{}

class RottenApple extends Gala{}

class Macintosh extends Apple{
public static void main(String[] args){
List<Fruit> apples = new ArrayList<Fruit>();
basket(apples);

List<Object> objects = new ArrayList<Fruit>();
basket(objects); // We can pass in any supertype of Apple
}

static void basket(List<? super Apple> list){
list.add(new Apple()); // we can only add Apple or its subtype
list.add(new Gala());
//list.add(new Fruit()); // we cannot add its supertype
}
}
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

I'm really confused about that.

Plese look at explanation extracted from K&B Study Guide for Java 5 about <? super ...> syntax :

Chapter 7, page 594

Now what you've said in this line
public void addAnimal(List<? super Dog> animals)

is essentially, "Hey compiler, please accept any List with a generic type that is of type Dog, or a supertype of Dog. Nothing lower in the inheritance tree can come in, but anything higher than Dog is OK."


But after trying a few Java codes, I could realize that is happening the opposite. Anything lower in the hierarchy is allowed but anything higher is not.

Please check out the following code :


As per above code you can see that with super I cannot add Dog's parent class but anything lower in the hierarchy is allowed.

After that I'm really confused because until now I think I was thinking in wrong way.

Could P L E A S E explain that.

Any comment would be H I G H L Y appreciated.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

After re-read the author's explanation I finally got it.

I made a mistake. I misunderstood the author's explanation.

Please ignore my previous post.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well if I must comment then it goes like this. list<? super apple> t tells the compiler that the variable t can hold a list of type that is super to apple or apple itself but also tells it that since it can hold a collection of apple and its fathers whom we dont know permit them to add only subtypes of apple and apple itself since in all the cases they will comply but remember one thing too if it is a treeset then all the elements must be same type
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic