Why do inner classes make private methods accessible?

前端 未结 4 810
难免孤独
难免孤独 2020-11-28 06:25

I don\'t understand why this compiles. f() and g() are visible from the inner classes, despite being private. Are they treated special specially because they are inner clas

4条回答
  •  伪装坚强ぢ
    2020-11-28 07:23

    I think this quote sums it up nicely:

    ...inner classes can access all members of the declaring class, even private members. In fact, the inner class itself is said to be a member of the class; therefore, following the rules of object-oriented engineering, it should have access to all members of the class.

    And following from that, since both inner classes are really just part of the containing class, they should be able to access each others private members as well.

提交回复
热议问题