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

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

Moderator: admin

Post Reply
demetrio
Posts: 16
Joined: Mon Sep 30, 2019 11:40 am
Contact:

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

Post by demetrio »

Please, can you elaborate again these two statements with more explanations?
Hierarchy 2 : A<T> <<< A<? super T> <<< A<? super S> Example: List<Number> is a subtype of List<? super Number> and List<? super Number> is a subtype of A<? super Integer> Thus, if an overridden method returns List<? super Number>, the overriding method can return List<Number> but not List<Integer> or List<? super Integer>.

It is important to understand that List<Integer> is not a subtype of List<Number> even though Integer is a subtype of Number.
I can understand easily
Hierarchy 1 : A<S> <<< A<? extends S> <<< A<? extends T> Example: Since Integer is a subtype of Number, List<Integer> is a subtype of List<? extends Integer> and List<? extends Integer> is a subtype of A<? extends Number>. Thus, if an overridden method returns List<? extends Integer>, the overriding method can return List<Integer> but not List<Number> or List<? extends Number>.
But I can't understand why:
1 - "List<? super Number> is a subtype of A<? super Integer>" instead of super type
2 - List<Integer> is not a subtype of List<Number> even though Integer is a subtype of Number

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

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

Post by admin »

Please go through Java Generics FAQs - Frequently Asked Questions by Angelika Langer. It will solve all your doubts about generics. Remember that it is a bit advanced for OCA/OCP Part 1 but you will need to learn it anyway later.
If you like our products and services, please help us by posting your review here.

alfredo.zuloaga
Posts: 10
Joined: Fri Nov 18, 2016 5:48 pm
Contact:

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

Post by alfredo.zuloaga »

This is also true
public ArrayList<? extends Number> getList(){

class A {
public List<Number> getList() {
return null;
}

}

class B extends A {
@Override
public ArrayList<? extends Number> getList() {
return new ArrayList<Integer>();
}
}

Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests