Java offers four access specifiers, listed below in decreasing accessibility:
- Public- public classes, methods, and fields can be accessed from everywhere.
- Protected- protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package.
- Default(no specifier)- If you do not set access to specific level, then such a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package.
- Private- private methods and fields can only be accessed within the same class to which the methods and fields belong. private methods and fields are not visible within subclasses and are not inherited by subclasses.
- SituationpublicprotecteddefaultprivateAccessible to class
from same package?yesyesyesnoAccessible to class
from different package?yesno, unless it is a subclassnono
No comments:
Post a Comment