Modifiers are keywords that you add to those definitions to change their meanings. Java language has a wide variety of modifiers, including the following −
use a modifier, you include its keyword in the definition of a class, method, or variable. The modifier precedes the rest of the statement.
Access Control Modifiers
Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are −
1. Visible to the package, the default. No modifiers are needed.
2. Visible to the class only (private).
3. Visible to the world (public).
4. Visible to the package and all subclasses (protected).
Non-Access Modifiers
Java provides a number of non-access modifiers to achieve many other functionality.
1. The static modifier for creating class methods and variables.
2. The final modifier for finalizing the implementations of classes, methods, and variables.
3. The abstract modifier for creating abstract classes and methods.
4. The synchronized and volatile modifiers, which are used for threads.
Types Of Access Specifier
1. Default
2. Public
3. Private
4. Protected
Default -
Default same pkg work as a public and outside pkg work as private.
Public -
Private -
Protected -
Table of Access Specifier
Same Sub Class Non Sub Class Sub Class Non Sub Class
Class Same pkg Same pkg Outside pkg Outside pkg
Private Yes No No No No
Public Yes Yes Yes Yes Yes
Default Yes Yes Yes No No
Protected Yes Yes Yes Yes No
0 Comments