Java Reference
In-Depth Information
the capabilities of the original class. This approach is a great alternative to the
dreaded suggestion, “I'll just copy that program and make a few changes.” Before
long, the developer will have two versions of the same program to maintain.
When one class inherits from another class, a relationship is defined between
the two. The original class is referred to as the base class or the superclass , and the
one that extends it is called the derived class or the subclass . Subclasses can them-
selves be inherited by other classes; and these new classes can also be inherited. A
typical object-oriented design presents a hierarchy of related base classes and de-
rived classes. As a matter of fact, Java itself is an object-oriented design: Everything
is inherited from the Object base class (see Figure 5.1).
FIGURE 5.1
The subclasses in this diagram inherit from various superclasses. All of these classes
ultimately inherit from the Java base class Object ( Java.lang.Object ).
To get the greatest benefit out of inheritance, a designer should attempt to or-
ganize class structures in advance (a “bottom-up” design). To do this, the designer
must analyze application requirements and recognize functions that are similar or
are likely to be reused. These must be further examined to abstract the common
functionality into an organized set of base classes. Then specializations of these
classes are designed and constructed to meet the unique application requirements
(a “top-down” implementation).
The process of defining the appropriate base classes and the appropriate class
hierarchy is one of the most challenging aspects of proper object-oriented design.
However, using a well-designed class hierarchy infrastructure is one of the simplest
and most efficient processes in object-oriented coding.
An object-oriented design uses inheritance for other benefits as well. It can
provide for efficient enhancements to a system. Modifications to a system need be
made only to the appropriate class in the class hierarchy; all derived classes will au-
tomatically benefit from this enhancement.
Search WWH ::




Custom Search