I have these classes:
package abc;
public class A {
public int publicInt;
private int privateInt;
protected int protectedInt;
int defaultInt;
public void test() {
publicInt = 0;
privateInt = 0;
protectedInt = 0;
defaultInt = 0;
}
}
"A" contains attributes of all four access modifiers. These other classes extend "A" or create instances and try to access attributes.
package de;
public class D {
public void test() {
E e = new E();
e.publicInt = 0;
e.privateInt = 0; // error, cannot access
e.protectedInt = 0; // error, cannot access
e.defaultInt = 0; // error, cannot access
}
}
package de;
import abc.A;
public class E extends A {
public void test() {
publicInt = 0;
privateInt = 0; // error, cannot access
protectedInt = 0; // ok
defaultInt = 0; // error, cannot access
}
}
package abc;
import de.E;
public class C {
public void test() {
E e = new E();
e.publicInt = 0;
e.privateInt = 0; // error, cannot access
e.protectedInt = 0; // ok, but why?
e.defaultInt = 0; // error, cannot access
}
}
Everything is ok, except I do not understand, why in class C, I can access e.protectedInt.