Java Reference
In-Depth Information
Classes were introduced in Chapter 8. Using classes, you can combine data and opera-
tions on that data in a single unit, a process called encapsulation. Through encapsulation, an
object becomes a self-contained entity. Operations can (directly) access the data, but the
internal state of an object cannot be manipulated directly.
In addition to implementing encapsulation, classes have other capabilities. For instance, you
can create new classes from existing classes. This important feature encourages code reuse
and saves programmers an enormous amount of time. In Java, you can relate two or more
classes in more than one way. This chapter examines two common ways to relate classes—
inheritance and composition (aggregation).
Inheritance
Suppose that you want to design a class , PartTimeEmployee ,toimplementandprocess
the characteristics of a part-time employee. The main features associated with a part-time
employee are the name, pay rate, and number of hours worked. In Example 8-8 (Chapter
8), we designed the class Person to implement a person's name. Every part-time
employee is a person. Therefore, rather than design the class PartTimeEmployee
from scratch, we want to be able to extend the definition of the class Person from
Example 8-8 by adding additional members (data and/or methods).
Of course, we do not want to make the necessary changes directly to the class
Person —that is, edit the class Person and add and/or delete members. We want to
create a new class PartTimeEmployee without making any physical changes to the
class Person , by adding only the members that are necessary to class
PartTimeEmployee . For example, because the class Person already has data members
to store the first name and last name, we will not include any such members in the class
PartTimeEmployee . In fact, these data members will be inherited from the class
Person . (We will design the class PartTimeEmployee in Example 10-3.)
In Chapter 8, we extensively studied and designed the class Clock to implement the
time of day in a program. The class Clock has three data members (instance variables)
to store the hours, minutes, and seconds. Certain applications might require that we also
store the time zone. In this case, we want to extend the definition of the class Clock and
create a class , ExtClock , to accommodate this new information. That is, we want to
derive the class ExtClock by adding a data member— timeZone —and the necessary
method members to manipulate the time.
In Java, the mechanism that allows us to extend the definition of a class without making
any physical changes to the existing class is inheritance. Inheritance can be viewed as an
''is-a'' relationship. For example, every (part-time) employee is a person. Similarly, every
extended clock, ExtClock , is a Clock .
Inheritance lets you create new classes from existing classes. Any new class that you create
from an existing class is called a subclass or derived class; existing classes are called
superclasses or base classes. The inheritance relationship enables a subclass to inherit
 
 
Search WWH ::




Custom Search