Java inheritance when both the child and parent classes are internal

Why does the private method of the parent class Base appear in the child class of the child in the code below?

 public class Trial { class Base { private void foo() { } } class Child extends Base { private void func() { super.foo(); } } } 

It would not be possible if the Base and Child classes were not inner classes. Why is this behavior for inner classes?

+7
java inheritance inner-classes
source share
1 answer

[If] a member or constructor is declared private, [then] access is allowed if and only if it occurs inside the body of a top-level class (ยง7.6), which includes the declaration of a member or constructor.

- https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#d5e9880

-one
source share

All Articles