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.
About Question enthuware.ocpjp.v11.2.3312 :
Moderator: admin
-
- Posts: 22
- Joined: Thu Dec 09, 2021 11:23 pm
- Contact:
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3312 :
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
"? 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.
-
- Posts: 34
- Joined: Mon Oct 30, 2023 11:55 am
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3312 :
Could you explain "subclass":
And, by the way "bV.add(new Dooby(){ });" looks also the right answer.
If I may "add a Dooby or its subclass object" in that case option "dV = tL;" should also be a right answer.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
And, by the way "bV.add(new Dooby(){ });" looks also the right answer.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3312 :
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?
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.
-
- Posts: 26
- Joined: Thu Aug 24, 2023 4:33 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3312 :
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:
It will not compile for the same reason why the last option does not compile.
I believe the reason is
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(){ });
Code: Select all
List<? extends Dooby> bV = null;
bV.add(new Dooby(){ });
I found it hard to pin from the explanation."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."
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3312 :
Sure, will update the explanation to make it more clear.
Thank you for your feedback!
Thank you for your feedback!
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 38 guests