Java Access Modifiers

An access modifier defines the scope of a class or its members.It determines the level of accessibility of the class or its member.

By using access modifier you can either allow or restrict accessibility of class, members, variables etc. 
 

Access modifiers supported in java 


*
Public
*
Private
*
Protected 
* 
Default


Lets get into details of each modifiers.

PUBLIC

When public access modifier is specified, then the respective entity can be accessed from anywhere in the application. 
 
 

PRIVATE

When private access modifier is mentioned, then the respective entity can be accessed only in the class in which they are defined. Even the other class of the same package will not be able to access it. 
 
 

PROTECTED

When protected access modifier is specified, then the respective entity can be accessed anywhere in its class and its derived class (can be in different package as well).
 
 

DEFAULT

This access modifier applies by default if no access modifier is specified for a class, members, variables etc. These entities can only be accessed with the SAME PACKAGE.


CHEETSHEET

 

 
DEFAULT
PRIVATE
PROTECTED
PUBLIC
With in Same Class   
yes
yes
yes
yes
Derived class with in the same PACKAGE   
yes
No
yes
yes
Non derived class with in the Same PACKAGE   
yes
No
yes
yes
Derived class  in Diff. PACKAGE   
No
No
yes
yes
Non Derived class  in Diff. PACKAGE   
No
No
No
yes

 


Print   Email