(^326) | Inheritance, Polymorphism, and Scope
7.1 Inheritance
Let’s look at an analogy between the work of an architect and the work of a programmer. The
way that an architect handles the complexity of a large building design sheds some light on
how we can organize our own programming work. This analogy lets us consider the same
concepts but without the distraction of Java syntax and semantics.
An Analogy
The architect begins by considering the overall requirements for a building: square footage,
number of occupants, types of usage, size of the building lot, height limits, and so on. After
making some initial decisions, the architect faces a basic aspect of any design: The building
is composed of floors. In many buildings, the floors all have common characteristics: the same
size and shape; the same number and locations for elevator shafts, stairways, and utility
trunks; and so on. The architect could begin by designing a basic empty floor with all of
these common elements in place. Once she installs this plan in the library of her computer
aided design (CAD) program, she can then use it repeatedly as the starting point for design-
ing each floor of the building.
The architect may further decide that the building has two main types of floors: office floors
and mechanical equipment floors.The office floors might be of two types: executive office space
and standard office space. Starting from the basic empty floor design, the architect adds com-
ponents such as lavatories and hallways to make an empty office floor. She can then add offices
and conference rooms to the empty space. Each of the four types of floor is thus derived from
the basic empty floor and added to the library (see Figure 7.1). Drawing the entire building then
becomes simply a matter of creating an instance of one of these four floor plans for each story.
The architect uses the same process to design the components that make up the floors.
She might design a basic type of office, with a door, windows, lights, heating, wiring, and so
forth, and then derive several types of offices from that one design. From a given type of of-
fice, such as secretarial, she might further refine the design into subtypes such as general
secretarial, secretary/receptionist, and executive secretary.
Creating hierarchies of designs simplifies the architect’s job. She begins each hierarchy
with the most general form of a building component, such as the basic empty floor, and then
derives new designs by adding details to the more general forms. The new designsinheritall
of the characteristics of the general form, saving the architect from having to redraw those
pieces they share in common. In some cases she replaces existing parts of a design, as when
she substitutes a wider door for a reception area than appears in the basic secretarial office.
The replacement partoverrideswhat was originally specified in the more general form.
In addition to the components of individual floors, the architect can specify character-
istics that are common to all floors, such as a color scheme. Each floor will then inherit these
general properties. Sometimes she hidesor deletes portions of the general properties, as
when she customizes the color scheme for a particular floor that has been rented in ad-
vance by a company with its own corporate colors. We will see later how inheritance, over-
riding, and hiding are formally defined mechanisms in Java.