In total there are 3 access modifiers available. But 4 access control. 4th access control will be given when you don’t specify any access modifier which is Default.
Access Control:
- Public
- Private
- Protected
- Default
Access Modifiers for Class
Class can use only two of the Access Modifier.
Public
Public means given class will be accessible from all the classes available
in Java world.
Default
Default means it will be accessible only from the classes inside package in
which it is defined.
Java Access Modifiers Table for Class
Visibility
|
Public Access Modifier
|
Default Access Modifier
|
Within Same Package
|
Yes
|
Yes
|
From Outside the Same
Package
|
Yes
|
No
|
Access Modifiers for Variable (Instance / Static Variable)
Variables are eligible for all 4 level of Modifier.- Default
- Public
- Protected
- Private
Note*: Before checking the
visibility of variable check the visibility of class first. If class is visible
only then variables visibility make any difference. If class is not visible
then no variable(Not even public) will be accessible.
Default
Variable with Default access will be accessible only from the classes inside
package in which it is defined. Any method in any class which is defined in
same package can access given variable in any way.(Via
Inheritance or Direct access)
Public
Public means given variable will be accessible
from all the class available in Java world. Any method in any class can access
given variable in any way.(Via Inheritance or Direct access)
Protected
Protected means it will be accessible only from the sub classes(Inside
/ Outside package) (Via Inheritance Only).Protected respect class subclass relation while Default doesn’t. This is the only difference between these 2 modifier(Protected & Default).
Private
Private means it will be accessible only from within class. No one can
access the given variable from outside class(Not even subclass).
Java Access Modifiers Table for Variable
Visibility
|
Public Access Modifier
|
Private Access Modifier
|
Protected Access Modifier
|
Default Access Modifier
|
Within Same Class
|
Yes
|
Yes
|
Yes
|
Yes
|
From Any Class in Same
Package
|
Yes
|
No
|
Yes
|
Yes
|
From Any Sub Class in Same
Package
|
Yes
|
No
|
Yes
|
Yes
|
From Any Sub Class from
Different Package
|
Yes
|
No
|
Yes(Only By Inheritance)
|
No
|
From Any Non Sub Class in
Different Package
|
Yes
|
No
|
No
|
No
|
Access Modifiers for Methods
Default
Default means it will be accessible only from the classes inside package in
which it is defined. Any method in any class which is defined in same package
can access given method in any way.(Via Inheritance or
Direct access)
Public
Public means given method will be accessible from
all the class method in Java world. Any method in any class can access given
method in any way.(Via Inheritance or Direct access)
depending on class level access.
Protected
Protected means it will be accessible only from
the sub classes(Inside / Outside package)
.
Note* Protected respect class subclass relation while Default doesn’t. This is the only difference between these 2 (Default & Protected)modifier.
Note* Protected respect class subclass relation while Default doesn’t. This is the only difference between these 2 (Default & Protected)modifier.
Private
Private means it will be accessible only from within class. No one can
access the given method from outside class(Not even subclass).
Java Access Modifiers Table for Method
Visibility
|
Public Access Modifier
|
Private Access Modifier
|
Protected Access Modifier
|
Default Access Modifier
|
Within Same Class
|
Yes
|
Yes
|
Yes
|
Yes
|
From Any Class in Same
Package
|
Yes
|
No
|
Yes
|
Yes
|
From Any Sub Class in Same
Package
|
Yes
|
No
|
Yes
|
Yes
|
From Any Sub Class from
Different Package
|
Yes
|
No
|
Yes(Only By Inheritance)
|
No
|
From Any Non Sub Class in
Different Package
|
Yes
|
No
|
No
|
No
|
No comments:
Post a Comment