”老爹将毕生所学的技能传授于儿子,儿子在“继承”到了,便可以使用了。儿子所使用的技能是他自己从他老子那里学来的,不是他老子的。有时候,老爹觉得有些技能“不好”或者是儿子目前所不需要的,那么不打算把这些技能传授于儿子(private)。因此,儿子是不会知道如何使用这些技能的。不过,儿子有时候也会觉得老子某些技能不好用,然后加以改进(覆盖)。当然,儿子没准也会自创一些技能,这些技能是他老子都不会的。“
这个段子比较形象,适合记忆。
附一段上java官方文档关于能不能继承private的文字:
Private Members in a Superclass
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.
这段话第一句就说了子类不能继承父类的私有成员。
但是这里所说的不能继承,意思是不能直接用sonclass.privatefather这种形式来调用父类的成员,但是可以通过父类public来间接访问其private成员。