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 Generics question from bonus exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics question from bonus exam" Watch "Generics question from bonus exam" New topic
Author

Generics question from bonus exam

Sital Kotamraju
Greenhorn

Joined: Mar 21, 2006
Posts: 13
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());}
gaurav singhal
Ranch Hand

Joined: Nov 18, 2005
Posts: 135
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

Joined: Mar 21, 2006
Posts: 13
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
}
}
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
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.


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
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.
mambe nanje
Ranch Hand

Joined: Feb 22, 2006
Posts: 31
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


Da Clone in programming world
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generics question from bonus exam