Brian Goetz made the following claim on the openjfx-compiler project mailing list:
Brian Goetz: In fact, I'll bet that many java developers would be surprised to learn what "protected" really means in Java. Even language experts get confused on these points.
So what does protected really mean? Let's find out if you know the answer.
Q: Will the following Java source files compile together? Will Main run without exceptions? If so, what will be printed?
// base/Base.java
package base;
public class Base {
protected int i;
public Base(int i) {
this.i = i;
}
}
// derived/Derived.java
package derived;
import base.*;
public class Derived extends Base {
public Derived(int i) {
super(i);
}
public void foo(Base b) {
System.out.println(b.i);
}
}
// Main.java
public class Main {
public static void main(String[] args) {
Derived d = new Derived(1024);
d.foo(d);
}
}
Today's semi-relaxed rules: no running the compiler, but you can consult the JLS.