About Question enthuware.ocpjp.v7.2.1487 :

Moderator: admin

Post Reply
D3x!

About Question enthuware.ocpjp.v7.2.1487 :

Post by D3x! » Mon Oct 29, 2012 2:15 am

1. addData1(List<? super Dooby> dataList) : This means that dataList is a List whose elements are of a class that is either Dooby or a super class of Dooby. We don't know which super class of Dooby. Thus, if you try to add any object to dataList, it has to be a assignable to Dooby. Thus, dataList.add(b); will be invalid because b is not assignable to Dooby. Further, if you try to take some object out of dataList, that object will be of a class that is either Dooby or a Superclass of Dooby. Only way you can declare a variable that can be assigned the object retrieved from dataList is Object obj. Thus, t = dataList.get(0); and b = dataList.get(0); are both invalid.
but "t" is of type Tooby which is a subclass of Dooby? surely you should in this case use the other reference which is the only reference of a superclass available, ie "b" which is of type Booby?

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

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by admin » Mon Oct 29, 2012 12:36 pm

D3x! wrote:
1. addData1(List<? super Dooby> dataList) : This means that dataList is a List whose elements are of a class that is either Dooby or a super class of Dooby. We don't know which super class of Dooby. Thus, if you try to add any object to dataList, it has to be a assignable to Dooby. Thus, dataList.add(b); will be invalid because b is not assignable to Dooby. Further, if you try to take some object out of dataList, that object will be of a class that is either Dooby or a Superclass of Dooby. Only way you can declare a variable that can be assigned the object retrieved from dataList is Object obj. Thus, t = dataList.get(0); and b = dataList.get(0); are both invalid.
but "t" is of type Tooby which is a subclass of Dooby? surely you should in this case use the other reference which is the only reference of a superclass available, ie "b" which is of type Booby?
As you said, Tooby is a subclass of Dooby, but the objects in the list are of some superclass of Dooby. You cannot assign a superclass object to a subclass reference.
I am not sure if I addressed your doubt correctly. If not, can you please specify more details about what you mean?
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

D3x!

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by D3x! » Tue Oct 30, 2012 12:02 am

The list is asking for a Type of Dooby or one of its super classes, but you assign "t" which is of type tooby, which is a subclass. before you even look at the following method and what it requires you haven't met this methods condition?

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

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by admin » Tue Oct 30, 2012 6:22 am

D3x! wrote:The list is asking for a Type of Dooby or one of its super classes, but you assign "t" which is of type tooby, which is a subclass. before you even look at the following method and what it requires you haven't met this methods condition?
I think I see your problem now.
Since Tooby is a subclass of Dooby, you can say that Tooby is-a Dooby or Tooby is-a any-superclass-of-Dooby.

Therefore, if the list expects elements which are of type Dooby or its superclass, Tooby will work because it is-a Dooby or its superclass. That is why dataList.add(t); is fine.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

D3x!

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by D3x! » Tue Oct 30, 2012 6:34 am

Ok I see, my misunderstanding, I forgot that it will take any sub type of the specified type and thought it meant it had to be of that type specifically and its sub classes

D3x!

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by D3x! » Tue Oct 30, 2012 8:23 am

D3x! wrote:Ok I see, my misunderstanding, I forgot that it will take any sub type of the specified type and thought it meant it had to be of that type specifically and its sub classes
it kind of makes me curious at to what is the point on having a upper bounds in the first place, why would you even bother using <? super Dooby> when you can then just say <Booby>

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

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by admin » Tue Oct 30, 2012 8:48 am

D3x! wrote: it kind of makes me curious at to what is the point on having a upper bounds in the first place, why would you even bother using <? super Dooby> when you can then just say <Booby>
I just added more explanation to this post that should anwser your question. (Please see the third post in that thread.)
If you like our products and services, please help us by posting your review here.

Crashtest
Posts: 18
Joined: Fri May 31, 2013 1:18 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by Crashtest » Wed Jun 04, 2014 4:28 am

The hardest type of question for me. I have read your Apple explanation a few times yet I still have a question here:
2. addData2(List<? extends Dooby> dataList)
This means that dataList is a List whose elements are of a class that is either Dooby or a subclass of Dooby. Since we don't know which subclass of Dooby is the list composed of, there is no way you can add any object to this list.
Could we not add objects of type Dooby to it (rather than a subtype)? I thought Dooby would always fulfill the requirement of this List.

Comparing to Apple bag example - List<? extends Dooby> is a bag of Apples (or for example FujiApples). But since we know it as a bag of Apples, why do we care about what we put inside as long as it is an Apple. It still will be a bag of Apples and if we iterate over the list of Apples we can always assign every apple taken out to Apple. Whether it is Apple or FujiApple it would always work after adding an Apple to it. Or not? :?:

Crashtest
Posts: 18
Joined: Fri May 31, 2013 1:18 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by Crashtest » Wed Jun 04, 2014 4:52 am

Crashtest wrote:The hardest type of question for me. I have read your Apple explanation a few times yet I still have a question here:
2. addData2(List<? extends Dooby> dataList)
This means that dataList is a List whose elements are of a class that is either Dooby or a subclass of Dooby. Since we don't know which subclass of Dooby is the list composed of, there is no way you can add any object to this list.
Could we not add objects of type Dooby to it (rather than a subtype)? I thought Dooby would always fulfill the requirement of this List.

Comparing to Apple bag example - List<? extends Dooby> is a bag of Apples (or for example FujiApples). But since we know it as a bag of Apples, why do we care about what we put inside as long as it is an Apple. It still will be a bag of Apples and if we iterate over the list of Apples we can always assign every apple taken out to Apple. Whether it is Apple or FujiApple it would always work after adding an Apple to it. Or not? :?:
I have run this program and can see now that indeed Apple bag of type List<? extends Dooby> is quite useless when it comes to adding things to it. It wouldn't take anything in. Can I assume, as a general rule with Generics, that for any <? extends XXX> I can only treat it as read-only? And as for <? super XXX> I can treat it as read-only (with casting to Object), and read-write if I want to write (add) item that is XXX or is-a XXX?

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

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by admin » Wed Jun 04, 2014 5:41 am

Crashtest wrote:The hardest type of question for me. I have read your Apple explanation a few times yet I still have a question here:
2. addData2(List<? extends Dooby> dataList)
This means that dataList is a List whose elements are of a class that is either Dooby or a subclass of Dooby. Since we don't know which subclass of Dooby is the list composed of, there is no way you can add any object to this list.
Could we not add objects of type Dooby to it (rather than a subtype)? I thought Dooby would always fulfill the requirement of this List.
The list must contain objects of a class that extends from Dooby, not of Dooby. Since you cannot assign objectOfDooby to a reference of SubClassOfDooby, you cannot put objectOfDooby in this list.
Comparing to Apple bag example - List<? extends Dooby> is a bag of Apples (or for example FujiApples). But since we know it as a bag of Apples, why do we care about what we put inside as long as it is an Apple. It still will be a bag of Apples and if we iterate over the list of Apples we can always assign every apple taken out to Apple. Whether it is Apple or FujiApple it would always work after adding an Apple to it. Or not? :?:
That logic is very good but the problem is the compiler also has to consider the fact that some one might pass a List<FujiApple> object to wherever List<? extends Apple> is required. For example:

Code: Select all

  ...
  ArrayList<FujiApple> faList = new ArrayList<FujiApple>();
  faList.add(new FujiApple());
  someMethod(faList);
  ...

  public void someMethod(List<? extends Apple> aList){
     //here, if you try to add MacintoshApple to aList, wouldn't that mess up aList? 

  }
If you like our products and services, please help us by posting your review here.

Crashtest
Posts: 18
Joined: Fri May 31, 2013 1:18 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by Crashtest » Wed Jun 04, 2014 5:47 am

Ok :idea:, makes sense now. Thank you for explanation.

Nisim123
Posts: 42
Joined: Mon Jan 20, 2014 2:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by Nisim123 » Thu Mar 10, 2016 1:11 pm

Yes, well actually I've read this topic the other day quite thoroughly, as well as the explanation
that we were directed to here
and actually recalled a long discussion about this subject in our favourite KS && BB guide.
Well to keep things short....
They raise there the question and ask why this kind of usage is allowed with arrays, meaning we can build an array
using a reference type of a super class and the actual object of its subtype as follows:

Code: Select all

Animal [ ] animals = new Dog[ 3 ] ;
and it is perfectly legal and compiles just fine, not to mention that a method can accept an array of Animals by its definition and take an array of one of its subtypes while running, and add to it a Dog object for instance.. and this is also perfectly legal,
So why doesn't it work with collections the same way?
why can't we just send a collection of Dogs to a method that takes Animals and its subtypes?
and the answer they give there is that the compiler can never know what kind of elements you might add to the collection that was sent to a method that takes a collection of Animals and its subtypes.
For instance lets say that we send a collection of Dogs to a method that takes a collection of Animals and its subtypes,
nothing can prevent us from adding a Cat to this collection of Dogs :
public void addAnimal(List<? extends Animal> animals){

animals.add(new Cat()) ; // Here we try to add a Cat element to a collection
// that might be a collection of only Dogs


}


And now back to the question why should it defer from the behaviour of arrays?
here they say that it is different with arrays since there is a special exception that can be thrown at runtime
(ArrayStoreException), while with generic collections we still have the compiler's type- erasure process that actually
cause a collection that was defined in a generic fashion to be seen by the compiler as it was declared in the legacy old fashioned way (that creates a collection of objects that we should use casting when trying to retrieve an element from it) .
The compiler use the type- erasure process for compatibility reasons with old codes (pre java 5...)
so that at runtime no exception can be thrown in case of adding a collection of one type an element of a wrong type.


I hope i got it right, was that true || false? :?:

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

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by admin » Thu Mar 10, 2016 10:14 pm

Yes, that is correct.
If you like our products and services, please help us by posting your review here.

Nisim123
Posts: 42
Joined: Mon Jan 20, 2014 2:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by Nisim123 » Sun Mar 13, 2016 8:42 am

And so if you say that it was correct, then I saw there another example out of the flow of their explanation that says that sometimes,
if we try to do the following it might work:
(Suppose the Dog class extends Animal class and so a Dog is a subtype of Animal)

Code: Select all

    class Animal{
                                  // lots of Animal general definitions and values

                           }

class Dog extends Animal{
                                                 // lots of Dog specific values...

                   }
  public class TestClass{
                                            public static void main(String [ ] args){

                                                            //### An optional code that can go here...

                                                         }

                     List getAlistOfAnimals(List <Animal> animalList){

                                        animalList.add(new Dog() ) // Here they say that this sometimes work......

                                     return animalList ;        
                               }

                      }
So is it possible to send a collection of <Dog> to a method that takes a collection of <Animal>
in other words can we code the above main method in a fashion something like this:

Code: Select all

                   List <Dog> aDogList = new ArrayList<>() ;
                 
                   List <Dog> anotherDogList = getAlistOfAnimals(aDogList );  


Well maybe i should have tried it myself prior to asking you the question,
but still is that possible anyhow? :?:

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

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by admin » Sun Mar 13, 2016 9:00 am

I am sorry but I am not really comfortable discussing some other authors content because I don't know the context in which that content exists. I would request you to contact the author about what they are talking about.
-Paul.
If you like our products and services, please help us by posting your review here.

Nisim123
Posts: 42
Joined: Mon Jan 20, 2014 2:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by Nisim123 » Sun Mar 13, 2016 9:49 am

OK sir, you are probably right! it might work only if we send the getAnimalList(List <Animal> AnimalList)
a list of Animal objects, then inside that method we might add other subtypes of Animal such as Dog (or Cat).
what confused me was the following code sample followed by the explanation:
( KS &&BB OCA/OCP Java SE 7 guide p.645: )
So if this is true and you can put Dog s into an ArrayList<Animal> , then why
can't you use that same kind of method scenario? Why can't you do this?

Code: Select all

    public void addAnimal(ArrayList<Animal> animals) {

                                       animals.add(new Dog()); // sometimes allowed...
                                

                                          }

Actually, you CAN do this under certain conditions. The previous code WILL
compile just fine IF what you pass into the method is also an ArrayList<Animal> .
This is the part where it differs from arrays, because in the array version, you
COULD pass a Dog[] into the method that takes an Animal[] .
The ONLY thing you can pass to a method argument of ArrayList<Animal>
is an ArrayList<Animal> ! (Assuming you aren't trying to pass a subtype of
ArrayList , since, remember, the "base" type can be polymorphic.)

( KS &&BB OCA/OCP Java SE 7 guide p.645 at the beginning )
Sorry for my misuderstanding......... :oops:

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by jagoneye » Mon Jan 09, 2017 8:27 am

The explanation has misleading words which confuses me everytime I go through similar question of ? super Classname.
elements are of a class that is either Dooby or a super class of Dooby. We don't know which super class of Dooby.
So here in this question superclass of Dooby is Booby. So according to this explaination the list can contain Dooby or Booby and you can add those in the list but that is totally wrong. Please change all the correspoding questions with answers to explanation wordings:
You can add objects of type Dooby or its subtypes which leads to the correct answer.
Just in simple words I'll sum it up.

Code: Select all

? extends Employee
extends means you can only read objects from it but you can't add any.
Here in this case you can read all Employees which can be a Manager or Clerk
all of which extend Employee(Here object read is guaranteed to be an Employee
hence you can assign the retreived object to Employee).

Code: Select all

? super Manager
super means you can add objects but cannot read from it(Here the object is guaranteed to be a Manager or Subtype so it will prevent adding lower level Employees to be added to the ranks of Manager).
Here you can add Manager or its subtypes say Executives to the Collection but
you cannot read objects from it.
HIH(Hope it helps).

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

Re: About Question enthuware.ocpjp.v7.2.1487 :

Post by admin » Mon Jan 09, 2017 11:51 am

Explanation is correct. You are misinterpreting the explanation. In no way does the sentence of the explanation that you've highlighted implies what you are saying i.e. "So here in this question superclass of Dooby is Booby. So according to this explaination the list can contain Dooby or Booby".

You might want to go through this to get a better understanding: viewtopic.php?f=2&t=473
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest