Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v6.1.824 )

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
Thadir
Posts: 5
Joined: Mon Aug 19, 2013 4:49 am
Contact:

Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v6.1.824 )

Post 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 :)

admin
Site Admin
Posts: 10402
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v6.1.824 )

Post by admin »

Thadir, Yes, fixed :)

pushpull
Posts: 14
Joined: Thu May 14, 2015 3:25 pm
Contact:

Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v

Post 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

Code: Select all

b = dataList.get(0);
is right. The least you can expect to get off the basket is an Apple. I think the answer

Code: Select all

dataList.add(t);
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

Code: Select all

Object o = dataList.get(0);
Or am I wrong?

admin
Site Admin
Posts: 10402
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v

Post 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.

pushpull
Posts: 14
Joined: Thu May 14, 2015 3:25 pm
Contact:

Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v

Post 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) ?

admin
Site Admin
Posts: 10402
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v

Post 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.

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v

Post 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?

admin
Site Admin
Posts: 10402
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests