Page 1 of 1
About Question com.enthuware.ets.scjp.v6._2_.100 :
Posted: Tue Nov 20, 2012 11:46 pm
by ETS User
I do not follow this. Can anyone explain ?
Re: About Question com.enthuware.ets.scjp.v6.2.100 :
Posted: Wed Nov 21, 2012 10:13 am
by admin
Can you please tell me which part of the explanation is not clear so that I can help?
-Paul.
Re: About Question com.enthuware.ets.scjp.v6.2.100 :
Posted: Wed Nov 21, 2012 12:42 pm
by yabmob
Wow, thanks for responding !
Given the following code, why does "dataList.add(t)" work and why does "dataList.add(b)" fail ?
I am obviously not understanding some basic concept here. My understanding is that "? super Dooby" would be any super class of Dooby and exclusive-of Dooby. In that case "dataList.add(b)" should work since only Booby is a super class of Dooby.
class Booby {}
class Dooby extends Booby {}
class Tooby extends Dooby {}
public class TestClass2 {
Booby b = new Booby() ;
Tooby t = new Tooby() ;
public void addData1(List<? super Dooby> dataList) {
dataList.add(t) ;
}
}
Re: About Question com.enthuware.ets.scjp.v6.2.100 :
Posted: Wed Nov 21, 2012 1:48 pm
by admin
? super Apple: This means "of a class that is a super class of Apple"
yabmob wrote:
My understanding is that "? super Dooby" would be any super class of Dooby and exclusive-of Dooby.
That is correct (except the exclusive of part), but you don't know which super class it is. It could be Object also and not necessarily Booby. That is why dataList.add(b) won't work.
You might want to go through
this brief write up to understand it.
HTH,
Paul
Re: About Question com.enthuware.ets.scjp.v6.2.100 :
Posted: Wed Nov 21, 2012 2:13 pm
by yabmob
I read that but its still confusing. Why does "dataList.add(t)" work ? Tooby is not a superclass of Dooby.
Re: About Question com.enthuware.ets.scjp.v6.2.100 :
Posted: Wed Nov 21, 2012 2:44 pm
by admin
yabmob wrote:I read that but its still confusing. Why does "dataList.add(t)" work ? Tooby is not a superclass of Dooby.
It works because you can add a subclass object to any collection of a super class. Since the list is of some super class of Dooby, you can add a Tooby to that list. For example, you can add a FujiApple to a bag of Apples or to a bag of Fruits.