Page 1 of 1

About Question enthuware.ocpjp.i.v11.2.3093 :

Posted: Fri Jul 19, 2019 10:46 am
by javabean68
Hi all,

would have option 4 been correct

Code: Select all

public ArrayList<? extends Integer> transform(Set<Integer> list)
if we had:

Code: Select all

ArrayList<? extends Number>...

as return type in Base?

Thanks in advance,
Bye :?
Fabio

Re: About Question enthuware.ocpjp.i.v11.2.3093 :

Posted: Fri Jul 19, 2019 8:52 pm
by admin
What happened when you tried it out?

Re: About Question enthuware.ocpjp.i.v11.2.3093 :

Posted: Sat Jan 14, 2023 4:50 am
by Val Martinez
I dont really catch why "ArrayList<? extends Integer> is not a subtype of ArrayList<Number>"

any type that extends from Integer, would be also a subtype of Number, because of its hierarchy relation. isn't it?

thanks in advance.

Re: About Question enthuware.ocpjp.i.v11.2.3093 :

Posted: Sun Jan 15, 2023 3:33 am
by Val Martinez
Val Martinez wrote:
Sat Jan 14, 2023 4:50 am
I dont really catch why "ArrayList<? extends Integer> is not a subtype of ArrayList<Number>"

any type that extends from Integer, would be also a subtype of Number, because of its hierarchy relation. isn't it?

thanks in advance.
Im sorry, I'll asnwer myself.

Already have studied it in the official docs, but its a bit counterintuitive. Integer, extends Number, but an Array of Integers doesnt extends an Array of Numbers, because simply, these types Array<Integer> and Array<Number> are not related on the Java hierarchy

Re: About Question enthuware.ocpjp.i.v11.2.3093 :

Posted: Sun Jan 15, 2023 8:15 am
by admin
List<Number> means, it is a list of Numbers. Every element in this list will be always a Number. For example, an element in this list could be an Integer or a Short or some other type of Number. We know for sure that whatever element is present in this list will be a Number. So, you can add an Integer or Short to it.

List<? extends Number> is a list of some class that extends Number but we don't know which type. For example, this list could be a list of Integers, in which case, it cannot contain a Short or some other type of Number. This list could also be a list of Shorts, in which case, it cannot contain Integers.
The same logic applies to List<? extends Integer>.

You can see that both types of list have different conditions on what they may contain. Therefore, List<? extends Number> or List<? extends Integer> is not a subtype of List<Number>.

Re: About Question enthuware.ocpjp.i.v11.2.3093 :

Posted: Sun Jan 15, 2023 8:19 am
by admin
You might want to check out this post: https://enthuware.com/forum/viewtopic.php?f=2&t=473