public class UnSafeSequence {
public class TestMath{
private TestMath(){
System.out.println("父类实例化");
}
}
public class TestMath1 extends TestMath{
public TestMath1(){
System.out.println("子类实例化");
}
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new UnSafeSequence().new TestMath1());
}
}
Description of private modifiers, definitions of top-level classes and inner classes in the Java 6 language specification
6.6.1 if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor. If a class If a member or constructor is declared private, then only the top-level class that declares the member or constructor has access (of course the class that declares the member and constructor is also accessible)
8. A top level class is a class that is not a nested class. A nested class is any class whose declaration occurs within the body of another class or interface. . The top-level class is not a nested class (inner class). A nested class (inner class) is a class declared in other classes or interfaces
In view of the above description, the external class can access the TestMath internal class whose constructor is marked as private. TestMath1 is also an internal class, which inherits another internal class TestMath. Because an internal class depends on the external class instance object to exist, it will be implicitly associated with an external class instance So
public class TestMath1 extends TestMath{
public TestMath1(){
System.out.println("子类实例化");
}
}
can be written as
public class TestMath1 extends UnSafeSequence.TestMath{
public TestMath1(){
UnSafeSequence.this.super();
System.out.println("子类实例化");
}
}
This can explain why a subclass of an inner class can access the private constructor of its parent class
Generally speaking, subclasses cannot call the private constructor of the parent class. The two classes you have here are both member inner classes of the same class. The inner class can freely access the member variables of the outer class, even if they are private. Therefore, a member inner class can access another member inner class (because it can be regarded as a member variable), and the accessed member inner class is completely unprotected from the member inner class accessing it.
Inner classes can inherently access the private methods and fields of any external class. The TestMath inherited by TestMath1 is itself the internal class of UnSafeSequence, so TestMath1 can access any private methods and fields defined in UnSafeSequence, including the private methods and fields in TestMath.
If you define TestMath separately outside UnSafeSequence, then TestMath1 cannot access the private methods and fields in TestMath.
Description of private modifiers, definitions of top-level classes and inner classes in the Java 6 language specification
In view of the above description, the external class can access the TestMath internal class whose constructor is marked as private.
TestMath1 is also an internal class, which inherits another internal class TestMath. Because an internal class depends on the external class instance object to exist, it will be implicitly associated with an external class instance
So
can be written as
This can explain why a subclass of an inner class can access the private constructor of its parent class
Generally speaking, subclasses cannot call the private constructor of the parent class.
The two classes you have here are both member inner classes of the same class. The inner class can freely access the member variables of the outer class, even if they are private. Therefore, a member inner class can access another member inner class (because it can be regarded as a member variable), and the accessed member inner class is completely unprotected from the member inner class accessing it.
Inner classes can inherently access the private methods and fields of any external class. The TestMath inherited by TestMath1 is itself the internal class of UnSafeSequence, so TestMath1 can access any private methods and fields defined in UnSafeSequence, including the private methods and fields in TestMath.
If you define TestMath separately outside UnSafeSequence, then TestMath1 cannot access the private methods and fields in TestMath.