Page 1 of 1
Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v6.1.824 )
Posted: Wed Aug 28, 2013 8:24 am
by Thadir
Just going trough this bc I keep messing up generics and I noticed the following
admin wrote:
3. ? super Apple: This means "of a class that is a super class of Apple". Therefore, List<? extends Apple> means a sack of something of which an Apple is a kind. Let's call it "it". Now play Jeopardy. So, What do you call a sack of something of which an Apple is a kind? A sack of fruits. Are you sure? Can you not call it a sack of food items? or A sack of Mcintoshes? (Hint: Every apple is a kind of fruit. Every apple is a kind of food item. Is every apple a kind of Mcintosh?) So which of these statements should work?
Shouldn't the red marketed
extends be
super?
And thanks translating this to dutch made a great mnemotechnique

Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v6.1.824 )
Posted: Wed Aug 28, 2013 8:41 am
by admin
Thadir, Yes, fixed

Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v
Posted: Tue May 26, 2015 9:29 am
by pushpull
Ok, i'm trying to understand this and i don't know what's happening. I've managed to change the Booby, Dooby and Tooby classes to Fruit, Apple and McIntosh. So we got:
Code: Select all
class Fruit {}
class Apple extends Fruit {}
class McIntosh extends Apple {}
public class TestClass {
Fruit b = new Fruit();
McIntosh t = new McIntosh();
public void addData1(List<? super Apple> dataList) {
//1
}
public void addData2(List<? extends Apple> dataList) {
//2
}
}
I think I get it, why in //2
is right. The least you can expect to get off the basket is an Apple. I think the answer
could be correct either, because you can put a McIntosh into to bag of Apples.
But this answer is happen to be placed in //1
And I just don't understand why? Why you can't put any Fruit into the bag of sth that an Apple is?
And as far I have understand the topic, you can't pull out anything that is more precise than an Object from this. So any answer with getting sth from the list, would be
Or am I wrong?
Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v
Posted: Tue May 26, 2015 10:29 am
by admin
I read your post but I am not sure I understand your question. Could please post the code that you expect to work but is not working or do not expect to work but is working?
Paul.
Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v
Posted: Tue May 26, 2015 5:05 pm
by pushpull
This is the code from the question that we need to complete:
Code: Select all
class Booby {}
class Dooby extends Booby {}
class Tooby extends Dooby {}
public class TestClass {
Booby b = new Booby();
Tooby t = new Tooby();
public void addData1(List<? super Dooby> dataList) {
//1
}
public void addData2(List<? extends Dooby> dataList) {
//2
}
}
These are the answers possible to put in lines //1 and //2:
Code: Select all
dataList.add(b);
dataList.add(t);
b = dataList.get(0);
t = dataList.get(0);
And these are the correct answers:
Code: Select all
//1 dataList.add(t);
//2 b = dataList.get(0);
My code changed the Booby, Tooby and Dooby part to Fruit and Apples, to let me better understand.
The question is, why those answers are correct (especially the 1st one) ?
Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v
Posted: Tue May 26, 2015 7:37 pm
by admin
In case of //1 (i.e. addData1(List<? super Apple> dataList)), dataList is a List of a class that is a super class of Apple. For example, it could be a Fruit or Object. Since t is a McIntosh is-a Fruit and McIntosh is-a Object, you can add it to a bag of Fruit or Object.
In case of //2 (i.e. addData2(List<? extends Apple> dataList)), dataList is a List of a class that extends Apple. It could be a McIntosh or some other subclass of Apple, but you don't know which one. Therefore, you cannot put a McIntosh into it. Therefore , dataList.add(t); is invalid. Since, what ever you take out of the bag, it will always be at least an Apple, b = dataList.get(0); is valid.
Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v
Posted: Mon Dec 07, 2015 4:09 pm
by sir_Anduin@yahoo.de
Hi, thanks for this topic, after reading it several times and trying out I still dont get it right...
1. "? extends Apple" - doens not mean a sack of apples, but a sack of a special sort of apples (or Apple), but you dont know! - so you cant put anything in it. Right?
And
2. "? super Apple" - does not mean a sack of anything that an Apple is, but what does it mean? Why cant you put new Object() or new Fuit() it in??? I dont get it. Why only Apple and Apple subclasses?
Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v
Posted: Tue Dec 08, 2015 8:15 pm
by admin
1. Right. Because it could be a sack of FujiApples and if you try to put some other Apples in it, your collection will be ruined.
2. ? super Apple means "of a class that is a super class of Apple". So it could be a collection of Fruit, Food, or even Object. You know only the fact that the collection is of some class of which Apple is a kind. i.e. where Apple satisfies is-a relationship. e.g. Apple is-a Fruit, Apple is-a Food, Apple is-a Object.
Since you don't know the exact type of that collection, you cannot put an Object. Think about it. What if the collection is a collection of Fruit? Can you put an Object instance (i.e. new Object() ) into that collection? What if you put an Object into it and pass it on to someone else who expects Apples? He will get an Object instance instead of Apple and will get a ClassCastException because as Object is not an Apple.
But you know that an Apple or any of its subclass will always be a Fruit, or a Food, or even an Object. That is why you can only put an Apple or any of its subclass object into the collection. So you can pass the collection to whoever who expects Apple, or Fruit or Food or Object in that collection.
HTH,
Paul.