Java Reference
In-Depth Information
Occasionally we want the derived class to have access to the base class mem-
bers. There are two basic options. The first is to use either public or package visible
access (if the base and derived classes are in the same package), as appropriate.
However, this allows access by other classes in addition to derived classes.
If we want to restrict access to only derived classes, we can make
members protected. A protected class member is visible to methods in a
derived class and also methods in classes in the same package, but not to
anyone else. 1 Declaring data members as protected or public violates the
spirit of encapsulation and information hiding and is generally done only as a
matter of programming expediency. Typically, a better alternative is to write
accessor and mutator methods. However, if a protected declaration allows you
to avoid convoluted code, then it is not unreasonable to use it. In this text, pro-
tected data members are used for precisely this reason. Protected methods are
also used in this text. This allows a derived class to inherit an internal method
without making it accessible outside the class hierarchy. Notice that in toy
code , in which all classes are in the default unnamed package, protected
members are visible.
A protected class
member is visible to
the derived class
and also classes in
the same package.
4.1.6 the constructor and super
If no constructor is
written, then a sin-
gle zero-parameter
default constructor
is generated that
calls the base class
zero-parameter
constructor for the
inherited portion,
and then applies
the default initial-
ization for any addi-
tional data fields.
Each derived class should define its constructors. If no constructor is written,
then a single zero-parameter default constructor is generated. This constructor
will call the base class zero-parameter constructor for the inherited portion
and then apply the default initialization for any additional data fields (mean-
ing 0 for primitive types, and null for reference types).
Constructing a derived class object by first constructing the inherited por-
tion is standard practice. In fact, it is done by default, even if an explicit
derived class constructor is given. This is natural because the encapsulation
1. The rule for protected visibility is quite complex. A protected member of class B is visible to
all methods in any classes that are in the same package as B . It is also visible to methods in
any class D that is in a different package than B if D extends B , but only if accessed through a
reference that is type-compatible with D (including an implicit or explicit this ). Specifically,
it is NOT VISIBLE in class D if accessed through a reference of type B . The following example
illustrates this.
1 class Demo extends java.io.FilterInputStream
2 { // FilterInputStream has protected data field named in
3 public void foo( )
4 {
5 java.io.FilterInputStream b = this; // legal
6 System.out.println( in ); // legal
7 System.out.println( this.in ); // legal
8 System.out.println( b.in ); // illegal
9 }
10 }
 
 
Search WWH ::




Custom Search