Access modifiers or visibility modifiers


Access modifiers or visibility modifiers are mainly four type in java they are as:

  • private
  • default
  • protected
  • public 
private:   if you are declare
a.      the members of the class as private then those members are allowed within the class only
b.      private members of the class are not allowed to use inside subclass also
Default:  if you are declare
a.       the members of the class without any specifiers, then those members may be assume as a the default member.
b.      Default member will be accessed within the class directly, within subclass directly and within the non subclass indirectly(with object)
Protected:
                        If you declare the members of the class as protected, you can access within the class directly, within non subclass indirectly.
 Note: There is no difference between default access and protected access with the same package.

Public  : Public members can be accessed anywhere in class, subclass, super class and non subclass.

Here we summarize the visibility of the keyword: public, private, protected, static and final, it is showing where these keywords are allowed and where are not allowed.


Private
Protected
Public
Static
Final
Class
X
X
X
Static variable
--
Instance variable
--
Static block
X
X
X
--
X
Instance block
X
X
X
--
X
Static method
--
Instance method
--
Local variable
X
X
X
X
  
NOTE:  Don’t consider local variable as default variable.

0 comments :

Post a Comment