All the access specifiers in Java
Summary of the
access specifiers
that we have seen so far
private
private
members (variables and methods)
are
accessible
in
methods
inside:
the
defining
class
(no specifier) or default/package
default/package
members (variables and methods)
are
accessible
in
methods
inside:
the
defining
class
a
class
in the same
package
as the
defining
class
protected
protected
members (variables and methods)
are
accessible
in
methods
inside:
the
defining
class
a
class
in the same
package
as the
defining
class
a
derived
class
public
public
members (variables and methods)
are
accessible
in
every
method
of
every
class
Diagram for
easy
memorization of access specifier rules
Here is a very handy
diagram
that shows the
access specifiers
and their range of
accessibility
:
Modifier
Defining class
Package
Derived class
World
private
Yes
No
No
No
no modifier
Yes
Yes
No
No
protected
Yes
Yes
Yes
No
public
Yes
Yes
Yes
Yes
Notice that:
There are
4 regions
:
Defining class
Package
that contains the defining class
Derived class
of the defining class
World
(everywhere)
There are
4 levels
of accessibility:
private
package
(no specifier)
protected
public
Note that:
The
next level
contains the
range
of the
previous level
.