About Question com.enthuware.ets.scjp.v6._2_.100 :
Moderator: admin
About Question com.enthuware.ets.scjp.v6._2_.100 :
I do not follow this. Can anyone explain ?
Last edited by admin on Wed Oct 08, 2014 12:10 pm, edited 1 time in total.
Reason: Scrambled qid
Reason: Scrambled qid
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.100 :
Can you please tell me which part of the explanation is not clear so that I can help?
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Wed Nov 21, 2012 12:31 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.100 :
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) ;
}
}
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) ;
}
}
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.100 :
? super Apple: This means "of a class that is a super class of Apple"
You might want to go through this brief write up to understand it.
HTH,
Paul
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.yabmob wrote: My understanding is that "? super Dooby" would be any super class of Dooby and exclusive-of Dooby.
You might want to go through this brief write up to understand it.
HTH,
Paul
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Wed Nov 21, 2012 12:31 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.100 :
I read that but its still confusing. Why does "dataList.add(t)" work ? Tooby is not a superclass of Dooby.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.100 :
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.yabmob wrote:I read that but its still confusing. Why does "dataList.add(t)" work ? Tooby is not a superclass of Dooby.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 56 guests