For a coding project I have a class containing a nested class. The nested class is subclassed within the same outer class. The intention is for the outer class to contain some instances of the nested class which it can hand to other instances of the outer class.
The nested subclass allows the outer class to modify the contents while its superclass allowes the contents to be read and some methods invoked. The superclass objects are thus handed to other objects to links the outer class objects in a chain.
The question I have concerns the access modifiers. Here is a minimalist code example:
abstract class OuterClass {
protected class NestedSuperClass<T> {
private T data;
public NestedSuperClass (T t) {
this.data = t;
}
public T getOutput() {
return data;
}
}
protected class NestedSubClass<T> extends NestedSuperClass<T> {
public NestedSubClass (T t) {
super(t);
}
protected void setOutput(T t) {
super.data = t;
}
}
}
When looking up some documentation I was confused by the ability to access the private field of the superclass not being mentioned anywhere. Is there any resource explaining why the subclass is allowed to modify the private field of the superclass in this way?
I am completely fine with this working. I also noticed that it seems to work with data being marked as protected instead of private and not using the super keyword. I am mostly interested in any documentation mentioning this ability of the super keyword. Thanks in advance.