Why is there no sub-class visibility modifier in Java?

前端 未结 3 1944
一整个雨季
一整个雨季 2020-12-13 01:52

On more than one occasion I have found myself desiring a variable visibility that is not possible in Java. I wanted certain members to be visible within their own class and

3条回答
  •  有刺的猬
    2020-12-13 01:55

    I suppose they want to avoid the added complexity by having a non-linear access hierarchy.

    You should have control over your package, so simply don't call these protected methods there.

    (By the way, protected is not quite the same as sub-class and package, as non-static protected methods (if not in the same package) can't be called on arbitrary objects of the declaring class, but only on objects of the subclass the code is in. (You can see this on Object.clone(), which can only be called by the class whose object is being cloned.))

提交回复
热议问题