Page 1 of 1

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

Posted: Wed Dec 11, 2019 11:38 am
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

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

Posted: Wed Dec 11, 2019 9:36 pm
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.

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

Posted: Wed Apr 14, 2021 6:01 pm
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>();
}
}