Ok so I thought the point of having protected fields was so that the variable was accessible by only the subclass and the class having the protected fields. Making objects of either the subclass or the superclass should not grant access to these fields. If I am correct, how come code like this is compiling correctly?
//superclass
public class SuperClass{
protected int x = 5;
}
//main class with main method
public class MainClass{
public static void main(String[] args) {
SuperClass a = new SuperClass();
a.x = 8;
System.out.println(a.a);
}
}
This will print out 8, which means I modified a protected variable outside of the classes which have them...