About Question enthuware.ocpjp.v8.2.1445 :

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

Moderator: admin

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

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by admin »

If you declare the result variable as List<Object>, think about what will happen. You can add any object of any class to the list referenced to by result. This will completely negate the constraint imposed by the declared return type of the method that says the returned list can only contain objects that satisfy "E extends CharSequence".

This will not be acceptable to the compiler.

You might want to go through this to get a better understanding - viewtopic.php?f=2&t=473

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

lenalena
Posts: 56
Joined: Tue Feb 21, 2017 4:24 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by lenalena »

I understand now, thank you.

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by horst1a »

I dont understand neither why output cannot be typed to Object?

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

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by admin »

Can you post some code that shows what you are trying to do?
If you like our products and services, please help us by posting your review here.

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by horst1a »

Its ocpjp v7.2.1445, correct answer is number three. In the explanation it says it cant be tied to object.

Why not, i thought object serves all needs.

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by horst1a »

Dont see why it cannot be typed to object, as said in the explanation.
It is question numer ocpjp v7.2.1445,

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

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by admin »

The explanation is talking about tying the returned list to list of objects i.e. List<Object>. You can't do that because you don't know the type of objects contained in the list. This is explained in the previous sentence of the explanation -
"The output type of the method has been specified as ///List<? super E>/// , which means that it is a List that contains objects of some class that is a super class of E. Here, E will be typed to whatever is being used for '///in///'. For example, if you declare ///ArrayList<String> in///, E will be String. "

All you know is that the list contains objects of some class that is a super class of E. Now, that class could be Object as well but you don't know that for sure. That is why you can't assign the return value to List<Object>.

I suggest you to go through the basic concepts of generics from a good book or at least read this small write up - viewtopic.php?f=2&t=473

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

thodoris.bais
Posts: 25
Joined: Sat Jun 03, 2017 4:56 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by thodoris.bais »

I think most of the people (myself inclusive) have the misconception that the following will be acceptable
List<Object> result;
because of the fact that Object is the super class of all classes.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by __JJ__ »

Hi
This is very interesting, and now that I see the explanation I can understand why I was struggling to see why so many apparently legal options weren't satisfactory.

My understanding is that there is a bound for E, and that consequently E or a subclass of E goes in (well, a List thereof) and that what comes out is whatever E actually went in or one of its superclasses - is that correct?

If so, are there any examples that you can think of offhand in widely used packages that do something like this? I'm just wondering where and why this kind of thing might be used.

TIA

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

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by admin »

__JJ__ wrote:
Fri Jul 27, 2018 5:10 pm
My understanding is that there is a bound for E, and that consequently E or a subclass of E goes in (well, a List thereof) and that what comes out is whatever E actually went in or one of its superclasses - is that correct?
Correct.
__JJ__ wrote:
Fri Jul 27, 2018 5:10 pm
If so, are there any examples that you can think of offhand in widely used packages that do something like this? I'm just wondering where and why this kind of thing might be used.
Since the the return type of this method has to by an untyped list, it kind of defeats the purpose of using generics. Therefore, I don't think you will see real world examples of something like this.
The purpose of the question is just to make sure you understand how generics work.
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by __JJ__ »

OK thank you.

thunder
Posts: 2
Joined: Tue Apr 23, 2019 10:25 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by thunder »

Hi,

unfortunately I cannot full understand the explanation: "The important concept here once the method returns, there is no way to know what is the exact class of objects stored in the returned List".
Does that mean due to type erasure, there is no way to specify which type the returned list will include?

Therefore:
List<CharSequence> in;
List<CharSequence> result;

Is not valid because the compiler cant assure that List<CharSequence> will be returned although E extends CharSequence and ? super E is also fullfilled?
Thanks in advance!

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

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by admin »

>Is not valid because the compiler cant assure that List<CharSequence> will be returned although E extends CharSequence and ? super E is also fullfilled?

Correct. If you have a List that can have objects of a class that extends CharSequence ( List<? extends CharSequence> ), then it could have String objects and StringBuilder objects (or some other class that extends CharSequence).

So, if you retrieve an object from this list, you cannot cast that object to String (because it could be StringBuilder object). You can only use it at a CharSequence.
If you like our products and services, please help us by posting your review here.

thunder
Posts: 2
Joined: Tue Apr 23, 2019 10:25 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by thunder »

Thanks for the quick response!
I can understand why I wouldn't be able to cast that object to String, however CharSequence can be used as you said, wouldn't then List<CharSequence> result be possible? Because it can contain CharSequence objects or subclasses?

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

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by admin »

You might want to go through these basics first:
https://enthuware.com/forum/viewtopic.php?f=2&t=473
If you like our products and services, please help us by posting your review here.

ninjafox
Posts: 2
Joined: Fri May 21, 2021 6:30 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by ninjafox »

One of the rules is that a generic type can't inherit another generic type. There is simply no relation between them

Code: Select all

List<Object> list = new ArrayList<String>(); // Compilation error
In the code above, we assume that since String is a subclass of Object, we are allowed to assign an object of type ArrayList<String> to a reference of type List<Object>. However, this is not true. Try to look at the variable of type List<Object> as an independent type. For instance, the object of type String can be assigned to a variable of type Object thanks to inheritance. But the object of type List<String> can't be assigned to a variable of type List<Object>, because generic types do not support inheritance. The super type and the sub type of any generic type is the generic type itself. Hence, only the following assignments are allowed

Code: Select all

List<Object> list = new ArrayList<Object>(); // OK
List<String> list = new ArrayList<String>(); // OK
List<Object> list = new ArrayList<>(); // OK since Java 1.7 (ArrayList<> here is implicitly ArrayList<Object>)
We may still apply inheritance when dealing with elements in a generic collection

Code: Select all

List<Object> list = new ArrayList<>();
list.add(new Object());
list.add(new String());
list.add(new StringBuilder());
Now, as for the method in the question

Code: Select all

public static <E extends CharSequence> List<? super E> doIt(List<E> nums) {...}
<E extends CharSequence> means "Some type E that implements CharSequence", which can be String, StringBuilder, StringBuffer, etc. Thus, we may pass List<String>, List<StringBuilder> and List<StringBuffer> to the method. Now we want to get the result from the method, and logically we may think that the List<Object> can hold anything we want, so we go on with the code like this

Code: Select all

List<String> listOfStrings = new ArrayList<>();
List<Object> result = doIt(listOfStrings); // Compilation error
This code fails because a variable of type List<Object> expects only an object of type List<Object>. The method states that it returns List<? super E>. That is "Any type that is a parent to type E (that implements CharSequence) or E itself". Can it be List<Object>? Yes, it can. But can it also be List<String>? Or List<StringBuilder>? Yes, it can. And as we already know Java can't allow an object of type List<String> to be assigned to a variable of type List<Object>. Hence the only three variable types that are allowed to hold a reference to List<? super E> are these

Code: Select all

List result1 = doIt(listOfStrings); // raw type can be of any type
List<?> result2 = doIt(listOfStrings); // <?> means anything 
List<? super String> result3 = doIt(listOfStrings); // bounded type does not violate the return type of the method
If you want to dive deep, read about covariant, contravariant and invariant types

basileu1
Posts: 2
Joined: Tue Aug 10, 2021 8:37 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1445 :

Post by basileu1 »

Hi,

If may I add my 2 cents on the crux of the topic. It has to do with polymorphism of types of generics.
If this does not compile:

Code: Select all

	List<Object> myObjects = new ArrayList<String>();
then this also cannot compile

Code: Select all

        ArrayList<String> in = new ArrayList<>();
        List<Object> result = doIt(in);

Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests