About Question enthuware.ocpjp.v11.2.3312 :

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
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: 10384
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

Tester
Posts: 33
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: 10384
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?

Badem48
Posts: 29
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: 10384
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!

gadsgadsx
Posts: 13
Joined: Fri Apr 12, 2024 11:09 pm
Contact:

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

Post by gadsgadsx »

Thats a really nice question indeed. Also, another concept that is confusing and I think is worth mentioning, is if the question had an option like below:

dv.add(new Tobby());

This is legal, because Tobby is-a Dobby, and dv is typed to "? super Dobby". That's why I think a question with this option should be created, because is not obvious. At first glance, when we read "? super Dobby" the first thought that comes to mind is that the list should not accept Dobby subclasses, only superclasses. But this is not correct, and dV can also take subclasses (as the explanation states).

Since this concept is mentioned only in explanation part (I dont recall seeing a question that really tests this), I would suggest creating one about it :)

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

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

Post by admin »

Sure. Will work on it.
Thank you for your feedback!

Post Reply

Who is online

Users browsing this forum: No registered users and 23 guests