About Question enthuware.ocpjp.v11.2.3312 :

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

Moderator: admin

Post Reply
yuir12
Posts: 22
Joined: Thu Dec 09, 2021 11:23 pm
Contact:

About Question enthuware.ocpjp.v11.2.3312 :

Post by yuir12 »

Hello,

I have a question.

Per below explanation: List<? extends Booby> bV implies that bV must point to a List of some class that extends Booby. Thus, it could be a List of Booby, Tooby, or some other class that extends Booby.

Does below extend Booby also?
class Dooby extends Booby{ }

Hence, the following should also be correct?
bV.add(new Dooby(){ });


Thanks.

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

Re: About Question enthuware.ocpjp.v11.2.3312 :

Post by admin »

No, bV.add(new Dooby(){ }); would not be correct because you don't know exactly which type of objects bv is meant to store. What if bV was meant to store Tooby objects? But here you are trying to add Dooby object into it. That would corrupt the list.

"? extends Booby" just means that you have some idea about what kind of objects are there in the list (they are all such objects that satisfy "is-a" relationship with Booby) but you don't have full idea about what exactly is there.

Please go through this for more explanation: viewtopic.php?f=2&t=473
If you like our products and services, please help us by posting your review here.

Tester
Posts: 34
Joined: Mon Oct 30, 2023 11:55 am
Contact:

Re: About Question enthuware.ocpjp.v11.2.3312 :

Post by Tester »

Could you explain "subclass":
Although we don't know which class that is, we can always add a Dooby or its subclass object to this List because that object will satisfy the is-a relation with Dooby, Booby, Object
If I may "add a Dooby or its subclass object" in that case option "dV = tL;" should also be a right answer.
And, by the way "bV.add(new Dooby(){ });" looks also the right answer.

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

Re: About Question enthuware.ocpjp.v11.2.3312 :

Post by admin »

You can add any object of type Dooby or its subclass to a listed pointed to be dV. This means, the following is valid:

dV.add(new Dooby());

But tL is a list of Tooby, which means the list pointed to by tL cannot contain a Dooby object. It can only contain objects of type Tooby or its subclass.

If you assign tL to dV that means both dV and tL are pointing to the same list. Now, if you try to add a Dooby object using the dV reference (because dV.add(new Dooby()); is valid), then that would corrupt the list pointed to by tL.

It is a contradiction. dV allows addition of Dooby but tL does not. How can both requirements be satisfied by the same list?
If you like our products and services, please help us by posting your review here.

Badem48
Posts: 26
Joined: Thu Aug 24, 2023 4:33 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3312 :

Post by Badem48 »

Hi,

First of all, I would like to say that this is a really good question; I had never considered this possibility before.

I believe the explanation is somewhat unclear and misses a key point. It would be more helpful if it detailed each option, explaining why it is legal or not

The first option explanation says "It is actually adding an instance of the anonymous class that extends Dooby to dV." however this is not the only reason why it is legal if we change the related part of the code this way:

Code: Select all

List<? super Tooby> dV = null;
        dV.add(new Dooby(){ });
It will not compile for the same reason why the last option does not compile.

Code: Select all

List<? extends Dooby> bV = null;
        bV.add(new Dooby(){ });
I believe the reason is
"The specific subtype of Booby is unknown. Because the compiler cannot guarantee what the specific subtype is (it could be Booby, Dooby, Tooby, or any other subclass of Booby), it prevents you from adding anything other than null to the list. This restriction ensures type safety because, at runtime, the list could be, for instance, a List<Tooby>, and adding a Dooby would then be inappropriate."
I found it hard to pin from the explanation.

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

Re: About Question enthuware.ocpjp.v11.2.3312 :

Post by admin »

Sure, will update the explanation to make it more clear.
Thank you for your feedback!
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 38 guests