Private members in Java inheritance

后端 未结 5 656
执笔经年
执笔经年 2020-12-01 06:43

I was told that for a Java subclass it can inherit all members of its superclass. So does this mean even private members? I know it can inherit protected members.

C

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 07:49

    Though https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.2 shows that Private Members are not inherited. Actually, it is inherited by subclass. When we use debuggers to trace variables, it will show the private members under the label of "inherited", so just try it. there is another post discussing this question, and most of them think not inherited, which misleads many people, including me at first.

提交回复
热议问题