Why can nested child classes access private members of their parent class, but grandchildren cannot?

andyf :

Probably similar to the question, Why can outer Java classes access inner class private members? or Access to superclass private fields using the super keyword in a subclass .

But there are some differences: the children class can access the private members of their parent (and only the nearest parent) class.

Given the sample code below:

public class T {

    private int t;

    class T1 {
        private int t1;

        public void test() {
            System.out.println(t);
        }
    }

    class T2 extends T1 {

        private int t2;

        public void test() {
            System.out.println(t);
            System.out.println(super.t1);
            System.out.println(this.t2);
        }
    }

    class T3 extends T2 {

        public void test() {
            System.out.println(t);
            System.out.println(super.t1); // NG: t1 Compile error! Why?
            System.out.println(super.t2); // OK: t2 OK
        }
    }
}
dimo414 :

Clever example! But it's actually a somewhat boring explanation - there's no visibility problem, you simply have no way of referring to t1 directly from T3 because super.super isn't allowed.

T2 can't access its own t1 field directly since it's private (and child classes don't inherit their parent's private fields), but super is effectively an instance of T1 and since it's in the same class T2 can refer to the private fields of super. There's just no mechanism for T3 to address the private fields of its grandparent class T1 directly.

Both of these compile just fine inside T3, which demonstrates that a T3 can access its grandparent's private fields:

System.out.println(((T1)this).t1);
System.out.println(new T1().t1);

Conversely this doesn't compile in either T2 or T3:

System.out.println(t1);

If super.super were allowed you'd be able to do this from T3:

System.out.println(super.super.t1);

if I'd define 3 classes, A, B, C, A having a protected field t1 and B would inherit from A and C from B, C could refer to As t1 by invoking super.t1 because it´s visible here. logically shouldn't the same apply to inner classes inheritance even if the field are private, because these private members should be visible due to being in the same class?

(I'm going to stick with OP's T1, T2, and T3 class names for simplicity)

If t1 were protected there'd be no problem - T3 could refer to the t1 field directly just like any subclass. The issue arises with private because a class has no awareness of its parent classes' private fields, and therefore can't reference them directly, even though in practice they are visible. That's why you have to use super.t1 from T2, in order to even refer to the field in question.

Even though as far as a T3 is concerned it has no t1 field it has access into T1s private fields by being in the same outer class. Since that's the case all you need to do is cast this to a T1 and you have a way to refer to the private field. The super.t1 call in T2 is (in essence) casting this into a T1 letting us refer to its fields.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=424446&siteId=1