0% found this document useful (0 votes)
49 views4 pages

Java Visibility Modifiers: Default Visibility Means That No Visibility Modifier Was Explicitly Used. Default

This document discusses Java modifiers for classes, interfaces, methods, and variables. It divides modifiers into two groups: visibility modifiers and other modifiers. It describes the effects of visibility modifiers on inheritance and access for different constructs. It provides an example to demonstrate how visibility modifiers work for a class with subclasses in different packages.

Uploaded by

Mouhamad Bazzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views4 pages

Java Visibility Modifiers: Default Visibility Means That No Visibility Modifier Was Explicitly Used. Default

This document discusses Java modifiers for classes, interfaces, methods, and variables. It divides modifiers into two groups: visibility modifiers and other modifiers. It describes the effects of visibility modifiers on inheritance and access for different constructs. It provides an example to demonstrate how visibility modifiers work for a class with subclasses in different packages.

Uploaded by

Mouhamad Bazzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

This appendix summarizes the modifiers that give particular characteristics to Java

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.

Modifier Classes and interfaces Methods and variables

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 Visible anywhere. Inherited by all subclasses of its class.


Accessible anywhere.

protected N/A Inherited by all subclasses of its class.


Accessible by any class in the same package as its class.

private Visible to the enclosing Not inherited by any subclass.


class only
Not accessible by any other class.

figure F.1 Java visibility modifiers


702 APPENDIX F java modifiers

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

figure F.2 A situation demonstrating Java visibility modifiers

The protected method b() is inherited by both C1 and C2. A method in y


could invoke x.b(), but a method in z could not. Furthermore, an object of any
class in package One could invoke x.b(), even those that are not related to class
P by inheritance, such as an object created from class Another1.
Method c() has default visibility, since no visibility modifier was used to
declare it. Class C1 inherits c(), but C2 does not. Therefore object y can refer to
the method c() as if it were declared locally, but object z cannot. Object y can
invoke x.c(), as can an object instantiated from any class in package One, such
as Another1. Object z cannot invoke x.c().
These rules generalize in the same way for variables. The visibility rules may
appear complicated initially, but they can be mastered with a little effort.

other java modifiers


Figure F.3 summarizes the rest of the Java modifiers, which address a variety of
issues. Furthermore, any given modifier has a different effect on classes, inter-
faces, methods, and variables. Some modifiers cannot be used with certain con-
structs and therefore are listed as not applicable (N/A).
704 APPENDIX F java modifiers

Modifier Class Interface Method Variable


abstract The class may con- All interfaces are No method body is N/A
tain abstract meth- inherently abstract. defined. The method
ods. It cannot be The modifier is requires implementation
instantiated. optional. when inherited.
final The class cannot be N/A The method cannot be The variable is a constant,
used to drive new overridden. whose value cannot be
classes. changed once initially set.
native N/A N/A No method body is neces- N/A
sary since implementation
is in another language.
static N/A N/A Defines a class method. It Defines a class variable. It
does not require an instan- does not require an instan-
tiated object to be invoked. tiated object to be refer-
It cannot reference non- enced. It is shared (com-
static methods or variables. mon memory space) among
It is implicitly final. all instances of the class.

synchro- N/A N/A The execution of the N/A


nized method is mutually exclu-
sive among all threads.
transient N/A N/A N/A The variable will not
be serialized.
volatile N/A N/A N/A The variable is changed
asynchronously. The
compiler should not
perform optimizations
on it.

figure F.3 The rest of the Java modifiers

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.

You might also like