1.

Access Modifiers in Java

Answer»

The accessibility or scope of a field, function, constructor, or class is defined by the access modifiers in Java. The access modifier can be used to adjust the access level of fields, constructors, methods, and classes.
Java access modifiers are divided into four categories as shown in the image below:



  • private: A private modifier’s access level is restricted to members of the class. It isn’t accessible outside of the class.

  • default: A default modifier’s access level is limited to the package. It’s not possible to get to it from outside the package. If you don’t indicate an access level, the default will be used.


  • protected: A protected modifier’s access level is both within and outside the package via a child class.


  • public: A public modifier’s access level is universal. It can be accessed from within and outside the class, and from within and outside the package.

The following table depicts the level of accessibility that each access modifier allows in Java.

Access Modifierwithin classwithin packageoutside package by subclass onlyoutside package
privateYesNoNoNo
defaultYesYesNoNo
protectedYesYesYesNo
publicYesYesYesYes



Discussion

No Comment Found