Java Reference
In-Depth Information
Notice how every use of
Book
is preceded with the
bookpack
qualifier. Without this spe-
cification,
Book
would not be found when you tried to compile
UseBook
.
Understanding Protected Members
Newcomers to Java are sometimes confused by the meaning and use of
protected
. As ex-
plained, the
protected
modifier creates a member that is accessible within its package and
to subclasses in other packages. Thus, a
protected
member is available for all subclasses
to use but is still protected from arbitrary access by code outside its package.
To better understand the effects of
protected
, let's work through an example. First,
change the
Book
class so that its instance variables are
protected
, as shown here: