The Different Levels of Access Control

Access Control in java



Access Control and Inheritance

One last issue regarding access control for methods involves subclasses. When you cre-

ate a subclass and override a method, you must consider the access control in place on

the original method.

As a general rule, you cannot override a method in Java and make the new method more

restrictively controlled than the original. You can, however, make it more public. The fol-

lowing rules for inherited methods are enforced:

* Methods declared public in a superclass also must be public in all subclasses.

* Methods declared protected in a superclass must either be protected or public

in subclasses; they cannot be private.

* Methods declared without access control (no modifier was used) can be declared

more private in subclasses.

* Methods declared private are not inherited at all, so the rules don’t apply.


相关文章: