If there is a generic type node<E>
with two operations like setData(E Type)
and E getData()
and let s0
be a instance of node
by node<? extends Number>
then why don't the compiler allow me to use s0.setData(Number Type)
at least? I can understand why other types are not allowed but why not allow the setData
to put in a Number type as we are sure that the type of node will be at least number?
My code looks like this:
class Node<E> {
private E data;
// ...
public void setData(E obj) { data = obj; } // (1)
public E getData() { return data; } // (2)
// ...
}
Node<? extends Number> s0 = new Node<Number>();
s0.setData(200); //this is not allowed why?
Node<Number> s0 = new Node<Number>();
s0.setData(100); //But this is allowed!