Java Reference
In-Depth Information
Display 7.9
Access Modii ers
package
somePackage;
public class
A
{
public int
v1;
protected int
v2;
int
v3.
//package
//access
private int
v4;
public class
B
{
can access v1.
can access v2.
can access v3.
cannot
access v4.
In this diagram, “access” means access
directly, that is, access by name.
public class
C
extends
A
public class
D
extends
A
public class
E
{
{
{
can access v1.
cannot
access v2.
cannot
access v3.
cannot
access v4.
can access v1.
can access v2.
can access v3.
cannot
access v4.
can access v1.
can access v2.
cannot
access v3.
cannot
access v4.
A line from one class to another means the lower class
is a derived class of the higher class.
If the instance variables are
replaced by methods, the same
access rules apply.
Package Access
If you do not place any of the modifiers
public
,
private
, or
protected
before an
instance variable or method definition, then the instance variable or method is said to have
package access
. Package access is also known as
default access
and as
friendly access
.
If an instance variable or method has package access, it can be accessed by name inside
the definition of any class in the same package, but not outside of the package.