367
must be defined before they are used to initialize other variables, and local variables
can shadow class variables (name precedence).
External scope rules specify where a member can be accessed outside of a class.
Java provides four levels of external access. The default level is package, which
extends access to all classes in the same package. Withprotectedaccess, derived
classes can access a member as well. A member withpublicaccess can be used by
any code that imports the class. Theprivateaccess level restricts access to only the
class containing the member.
In this chapter, we learned how to declare derived classes. The advantage of a de-
rived class is that we can use the interface from the superclass as the model for our
design. We benefit from inheriting many fields and methods that give our derived
class powerful capabilities with no extra work on our part. In some cases, however, a
new object is sufficiently different from anything that exists in the standard hierar-
chy that we have to design it from the ground up. Now that you know how to design
and implement both derived classes and top-level classes, you are prepared to
explore a wide range of useful and general abstractions that computer scientists
have identified as fundamental to the development of more advanced algorithms.
The remainder of this text primarily examines some of these abstractions and their
implementation.
Objects can be written to and read from a file. They can be written on a file of the
class ObjectOutputStreamand read from a file of the class ObjectInputStream. Only ob-
jects of classes that implement the Serializableinterface can be written on object
files. Object files are used when the output from one application is used as input into
another application.
Quick Check
1.What is the most general class in Java? (p. 332)
2.What is the mechanism that allows one class to extend another class?
(pp. 328–333)
3.What do you call a class that is an extension of another class in the hierarchy?
(p. 330)
4.Overriding refers to _________ methods; hiding refers to fields and ________
methods. (p. 336)
5.What kinds of members are notinherited by a derived class? (pp. 333–336)
6.What is an operation that has multiple meanings depending on the object to
which it is applied? (p. 337)
7.Are constructors inherited? (pp. 333–336)
8.A reference to an instance of a _______ class can be assigned to a variable of its
__________ class. (p. 332)
9.Explain the meaning of the keyword extends. (p. 337)