7.3 How to Read a Class Hierarchy | 333
class when we can simply extend an existing class. As you look for things that are familiar
in solving a problem, keep in mind the existing classes.
In our address book example, we collected responsibilities from similar classes to form
a superclass. If we need to represent international telephone numbers, we could create a com-
pletely new class, but a better solution is create a subclass of Phonewith a field for the coun-
try code. Whenever you encounter such a situation, you should immediately consider how
you can take advantage of it through inheritance.
7.3 How to Read a Class Hierarchy
In Java, all classes can eventually trace their roots back to theObjectclass, which is so general
that it does almost nothing: Objects of typeObjectare nearly useless by themselves. ButObject
does define several basic methods: comparison for equality, conversion to a string, and so on.
The Java library defines numerous classes that directly extend the Objectclass and thus
inherit all of its methods. For example, a class called Componentextends Object
with the basic operations needed to display something in a window on the screen.
We can’t instantiate objects of the class Componentdirectly, however, because they
are incomplete. Java calls such classes abstract. Returning to our analogy, the ar-
chitect would never include the basic empty floor in a building plan, but uses it
instead to design floors that are complete. Similarly, we would never instantiate
an object of an abstractclass in our code, but we can use it as the superclass for
defining new subclasses that are complete.
In Chapter 8, we will see how to create a graphical user interface consisting
of a window on the screen, with buttons, labels that hold output, and boxes in
which a user can enter data values. As a preview of the graphical user interface
we introduce in Chapter 8, and because it provides a good example of the use
of inheritance, let’s look at the hierarchy of a class that allows us to create a data
entry field within a window.
Being familiar with how Java functions, you should not be surprised to learn
that Java provides a class called JTextFieldthat is used to create input fields in
a window.JTextFieldis derivedfrom a class called JTextComponent, which is in
turn derived from a class called JComponent, which is in turn derived from a class
called Container, which is in turn derived from Component. As you can see, the hi-
erarchy of classes can be many levels in depth, so it can become difficult to
keep track of which class is descended from which class. Figure 7.3 shows how
these classes are related. We use boxes with their corners cut out to represent
the incomplete nature of an abstractclass.
When the architect looks at a floor plan with her CAD program, she sees all
of its parts, including those that are derived from the basic empty floor. When
we look at the documentation for a Java class, however, we see only those fields
that are added by that specific class and the name of its superclass.
As an example, let’s look at a summary of the methods in the JTextField
class. Java class summaries (documentation) are typically written as method
headings. This summary includes a modifier,protected, that we have not yet
Abstract A modifier of a class
or field that indicates it is incom-
plete and must be fully defined
in a derived class
Derived class A class that is
created as an extension of
another class in the hierarchy
Object
Component
Container
JComponent
JTextComponent
JTextField
Figure 7.3 Hierarchy of
ComponentClasses above
JTextField