7

Why is the private method of the parent class Base visible in the child class Child in the code below?

public class Trial {

    class Base {
        private void foo()
        {
        }
    }

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

It wouldn't be possible if Base and Child classes were not inner classes. Why is this behaviour for inner classes?

nbrooks
  • 18,126
  • 5
  • 54
  • 66
victini
  • 193
  • 1
  • 6

1 Answers1

0

[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.

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

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
  • This IS the only correct answer! It is so because it is specified this way. One has to read the quote very carefully. The modifier "private" always refers to the body of the enclosing TOP LEVEL class as "scope of permitted access". – A Dude Mar 29 '18 at 09:11