I have a class CircularBuffer which extends AbstractList.
I have a second class ExponentialUnrolledLinkedList which has an inner class Node which extends CircularBuffer.
I can access some protected members of instances of Node in ExponentialUnrolledLinkedList (outside of Node), even if they are declared outside of Node (for example, the protected size field declared in CircularBuffer), but not the protected field modCount declared in AbstractList.
I can access modCount from the inner class definition itself, but not from the enclosing class.
For example
public class ExponentialUnrolledLinkedList<E> extends AbstractSequentialList<E> {
...
private Node last = new Node(1){
protected int firstIndex(){
return 0;
}
};
private class Node extends CircularBuffer<E> {
...
private void dirty(){
this.modCount++; // works
}
protected int firstIndex(){
return store.length;
}
}
public int size(){
return last.firstIndex() + last.size; // access to last.size works
}
public boolean add(E item){
...
last.modCount++; // can't access modCount, have to call last.dirty()
...
return true;
}
}
Why can I access size in both the inner class and the outter class, but I can only access modCount in the inner class? I was under the (obviously false) impression that outter classes have access to everything that their inner classes have access to. Clearly some protected members are accessible to both but some aren’t. I have a work-around by wrapping the modCount increment in a (private) method and calling that method, but this seems inelegant.
What is the visibility of CircularBuffer.size? (It is not a member of AbstractList or its super classes).
You can’t access CircularBuffer.modCount because its visibility is “protected” in the class AbstractList. This means the member can only be accessed within its own package and by a subclass of its class in another package (such as CircularBuffer).