Java Visibility Modifiers: Default Visibility Means That No Visibility Modifier Was Explicitly Used. Default
Java Visibility Modifiers: Default Visibility Means That No Visibility Modifier Was Explicitly Used. Default
classes, interfaces, methods, and variables. For discussion purposes, the set of all
Java modifiers is divided into two groups: visibility modifiers and all others.
F
java modifiers
java visibility modifiers
The table in Fig. F.1 describes the effect of Java visibility modifiers on various
constructs. Some relationships are not applicable (N/A). For instance, a class can-
not be declared with protected visibility. Note that each visibility modifier oper-
ates in the same way on classes and interfaces and in the same way on methods
and variables.
Default visibility means that no visibility modifier was explicitly used. Default
visibility is sometimes called package visibility, but you cannot use the reserved
word package as a modifier. Classes and interfaces can have default or public vis-
ibility; this visibility determines whether a class or interface can be referenced out-
side of its package. Only an inner class can have private visibility, in which case
only the enclosing class may access it.
When applied to methods and variables, the visibility modifiers dictate two
specific characteristics:
◗ Inheritance, which determines whether a method or variable can be refer-
enced in a subclass as if it were declared locally.
◗ Access, or the degree of encapsulation, which determines the scope in which
a method or variable can be directly referenced. All methods and variables
are accessible in the class in which they are declared.
default (no modifier) Visible in its package. Inherited by any subclass in the same package as its class.
Accessible by any class in the same package as its class.
Public methods and variables are inherited by all subclasses and can be
accessed by anyone. Private methods and variables are not inherited by any sub-
classes and can only be accessed inside the class in which they are declared.
Protected visibility and default visibility (no modifier) vary in subtle ways.
Note that a subclass of a parent may or may not be in the same package as the
parent, and that not all classes in a package are related by inheritance.
Protected methods and variables are inherited by all subclasses, whether they
are in the same package as the parent or not. Access to protected methods and
variables is given to any class in the same package as the class in which they are
declared. Therefore a subclass in a different package will inherit the protected
methods and variables, but the subclass cannot directly reference them in an
instance of the parent. Furthermore, a class can directly access a protected
method or variable that is declared in another class in the same package, whether
the two classes are related by inheritance or not.
A method or variable with default visibility is inherited only by subclasses that
are in the same package as the class in which the method or variable is declared.
A method or variable with default visibility can be accessed by any class in the
same package, whether they are related by inheritance or not.
All methods and variables declared in a parent class exist for all subclasses but
are not necessarily inherited by them. For example, when a child class is instan-
tiated, memory space is reserved for a private variable of the parent class.
However, that child class cannot refer to that variable by name since the variable
was not inherited. The child class can, however, call an inherited method that ref-
erences that variable. Similarly, an inherited method can invoke a method that the
child class cannot call explicitly. For this reason, inheritance is carefully defined
using the words “as if it were declared locally.” Noninherited methods and vari-
ables can still be referenced indirectly.
a visibility example
Consider the situation depicted in the Fig. F.2. Class P is the parent class that is
used to derive child classes C1 and C2. Class C1 is in the same package as P, but
C2 is not. Class P contains four methods, each with different visibility modifiers.
One object has been instantiated from each of these classes.
The public method a() has been inherited by C1 and C2, and any code with
access to object x can invoke x.a(). The private method d() is not inherited
by C1 or C2, so objects y and z have no such method available to them.
Furthermore, d() is fully encapsulated and can only be invoked from within
object x.
APPENDIX F java modifiers 703
package One
class P
P x = new P();
public a()
protected b() C1 y = new C1();
c()
private d() C2 z = new C2();
class Another1
package Two
class C1
class C2
class Another2
The transient modifier is used to indicate data that need not be stored in a
persistent (serialized) object. That is, when an object is written to a serialized
stream, the object representation will include all data that is not specified as tran-
sient. See Chapter 8 for a more detailed description.