It is implicitly assumed that all classes in the package “know” each other (because they were written by the same person / company / organization). Therefore, they either do not get access to the protected fields, or, if they do, they know how to do it correctly.
It is assumed that classes in one package are more related to each other than the parent to a derived class, because a derived class can actually be written by someone else. Therefore, they decided that confidential protection was more limited than protected.
So, I think you should not worry about how classes in one package can access each other's fields. In general, I just do not use this function, unless I write iterators.
If you have two fields, you can make them inner classes so that they have access to private fields (again, the logic is: if a class is inside another class, it knows about the semantics of this class) and can provide access to their derived classes through protected methods.
Of course, you can come up with some kind of complex token exchange protocol to make this field available only for B / C instances, but that would be a wonderful overhead, and another object can still use reflection to access all private members if you do not disable it with security policies, which is usually not the case, but again, security policies are ultimately decided by the owner of the JVM.
So, in the end, the preferred way to do what you say in Java is to either put them in the same package, or write B and C as inner classes of A so that they can directly access the private members of A and expose them their derived classes.
public class A { public static abstract class B { protected Whatever getWhatever(A a) { return ab; } protected void setWhatever(A a, Whatever value) { ab = value; } } public static abstract class C { protected Whatever getWhatever(A a) { return ac; } protected void setWhatever(A a, Whatever value) { ac = value; } } private Whatever b; private Whatever c; }
again, you always assume that classes in one package will never do anything wrong.