About Question enthuware.ocpjp.v7.2.1120 :

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

Moderator: admin

Post Reply
jimi24
Posts: 5
Joined: Thu Jan 02, 2014 3:36 am
Contact:

About Question enthuware.ocpjp.v7.2.1120 :

Post by jimi24 »

Option 2: return type of m4 is List<? extends Shape>.
And it is returning list (List<Shape>).

Is it ok to return List<Shape> when it is expected to return List<? extends Shape> ?
I think returning sub-type should be fine but here that's not the case and should give compilation error for it...

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

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

Post by admin »

No, here "extends Shape" doesn't necessarily mean that the return value has to be a subclass of Shape. It just means that the return value must be assignable to Shape. Therefore, the return value can be List<Shape> or a List of any subclass of Shape.

You might want to try it out.
-Paul.
If you like our products and services, please help us by posting your review here.

erbegu
Posts: 5
Joined: Thu Mar 06, 2014 9:29 am
Contact:

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

Post by erbegu »

If this is ok:
List<Shape> when it is expected to return List<? extends Shape>

then this has also to be OK ?

return List<Number> when expecting List<Integer> ?

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

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

Post by admin »

This is not really a small topic that can be explained here. You might want to read it from a book or check out the first sticky topic of this forum. viewtopic.php?f=2&t=473
If you like our products and services, please help us by posting your review here.

shamran99
Posts: 15
Joined: Wed May 10, 2017 2:49 am
Contact:

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

Post by shamran99 »

Hi,

Please explain me the following.

Code: Select all

List<? extends Shape> typeList = new ArrayList<>();
List<Shape> shapeList = new ArrayList<>();

typeList.addAll(shapeList); //1
typeList = shapeList; //2
Here 'line 1' will not compile because typeList is defined to take some class that extends Shape. However the compiler doesn't know which class. Therefore the compilation fails.

The same logic applies to 'line 2'. How shapeList can be assigned to typeList? Why this line is not giving compilation error?

Regards,
Shamran.

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

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

Post by admin »

shamran99 wrote:Here 'line 1' will not compile because typeList is defined to take some class that extends Shape. However the compiler doesn't know which class. Therefore the compilation fails.

The same logic applies to 'line 2'. How shapeList can be assigned to typeList? Why this line is not giving compilation error?

Regards,
Shamran.
You are not applying the logic correctly. A List of Shape is-a List of a class that extends Shape (because a Shape is-a Shape). So the compiler has no issue in assigning shapeList to typeList. Compiler has an issue when you try to add a Shape object to a List that says it is of a class that extends Shape because while adding the object, it looks at the type of the variable, which is List<? extends Shape> so even if the actual list pointed to by the variable is of type List<Shape>, the compiler doesn't know that.

Another way to look at it is - List<? extends Shape> promises that whenever you take out an object from this list, that object will be a Shape (could be just a Shape or a subclass of Shape). Assigning shapeList to typeList doesn't break this promise. So the compiler is ok.
If you like our products and services, please help us by posting your review here.

dongyingname
Posts: 18
Joined: Sat Jun 22, 2019 4:10 pm
Contact:

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

Post by dongyingname »

Last option: list is a List of some class that extends from Shape. It may not necessarily be Shape. Therefore, you cannot add a Shape to list.
This doesn't make sense to me. A list<? extends Shape> can contain elements of type Shape or of type Shape's subclasses.
Can anyone one give a better explanation?

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

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

Post by admin »

>A list<? extends Shape> can contain elements of type Shape or of type Shape's subclasses.
No, that is not what it means. List<? extends Shape> means it is a list that contains objects of a class that extends Shape. But you don't know which class.
You might want to read about generics from a good book or try this article to clear your understanding: https://enthuware.com/resources/oracle- ... g-generics
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 62 guests