Ok guys. This is a revision
class Node<E> { // (1)
private E data; // Data (2)
private Node<E> next; // Reference to next node (3)
Node(E data, Node<E> next) { // (4)
this.data = data;
this.next = next;
}
public void setData(E e) {} // (5)
public void xxxData(E e) {} // (6)
public E getData(E e) {return null;} // (7)
public static void main(String [] args) {
Node<? extends Integer> n1 = new Node<Integer>(1,null); //8
Node<? super Integer> n2 = new Node<Integer>(1,null); //9
n1.setData(new Integer(1)); //10 compiler error
n1.xxxData(new Integer(1)); //11 compiler error
n2.setData(new Integer(1)); //12 ok
}
}
Here's a rewrite hopefully i can convey my confusion nicely.
1. n1 is upper bounded wildcard. So this wont allow adding of records. Clause 10 proves this.
2. clause 11 also proves that method names (in this case 'SET' to determine adding of records) not being used since xxxData method gives the same compiler error.
3. n2 is lower bounded wildcard. Since method names doesn't play a role here, how does compiler knows that setData method can be used on n2 (since n2 is a lower bounded wildcard and this wildcard allows for adding of records)? Basically what the difference on method setData on clause 10 and 12?
Bear in mind nothing happens in those methods. It's empty or returning null.