About Question com.enthuware.ets.scjp.v6._2_.100 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question com.enthuware.ets.scjp.v6._2_.100 :

Post by ETS User »

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

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

Re: About Question com.enthuware.ets.scjp.v6.2.100 :

Post by admin »

Can you please tell me which part of the explanation is not clear so that I can help?
-Paul.
If you like our products and services, please help us by posting your review here.

yabmob
Posts: 5
Joined: Wed Nov 21, 2012 12:31 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.100 :

Post 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) ;
}
}

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

Re: About Question com.enthuware.ets.scjp.v6.2.100 :

Post 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
If you like our products and services, please help us by posting your review here.

yabmob
Posts: 5
Joined: Wed Nov 21, 2012 12:31 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.100 :

Post by yabmob »

I read that but its still confusing. Why does "dataList.add(t)" work ? Tooby is not a superclass of Dooby.

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

Re: About Question com.enthuware.ets.scjp.v6.2.100 :

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 56 guests