Java Reference
In-Depth Information
9.1 Creating Subclasses
In our introduction to object-oriented concepts in Chapter 1. we presented the
analogy that a class is to an object what a blueprint is to a house. In subsequent
chapters we've reinforced that idea, writing classes that define a set of similar
objects. A class establishes the characteristics and behaviors of an object but
reserves no memory space for variables (unless those variables are declared as
static ). Classes are the plan, and objects are the embodiment of that plan.
Many houses can be created from the same blueprint. They are essentially
the same house in different locations with different people living in them. Now
suppose you want a house that is similar to another but with some different or
additional features. You want to start with the same basic blueprint but modify
it to suit new, slightly different, needs. Many housing developments are created
this way. The houses in the development have the same core layout, but they have
unique features. For instance, they might all be split-level homes with the same
basic room configuration, but some have a fireplace or full basement while others
do not, or an upgraded gourmet kitchen instead of the standard version.
It's likely that the housing developer commissioned a master architect to create
a single blueprint to establish the basic design of all houses in the development,
then a series of new blueprints that include variations designed to appeal to differ-
ent buyers. The act of creating the series of blueprints was simplified since they all
begin with the same underlying structure, while the variations give them unique
characteristics that may be important to the prospective owners.
Creating a new blueprint that is based on an existing blueprint
is analogous to the object-oriented concept of inheritance , which
is the process in which a new class is derived from an existing one.
Inheritance is a powerful software development technique and a
defining characteristic of object-oriented programming.
Via inheritance, the new class automatically contains the variables and
methods in the original class. Then, to tailor the class as needed, the program-
mer can add new variables and methods to the derived class or modify the
inherited ones.
KEY CONCEPT
Inheritance is the process of deriving
a new class from an existing one.
In general, new classes can be created via inheritance faster, easier,
and cheaper than by writing them from scratch. Inheritance is one
way to support the idea of software reuse. By using existing software
components to create new ones, we capitalize on the effort that went
into the design, implementation, and testing of the existing software.
Keep in mind that the word class comes from the idea of classifying groups
of objects with similar characteristics. Classification schemes often use levels
of classes that relate to each other. For example, all mammals share certain
KEY CONCEPT
One purpose of inheritance is to
reuse existing software.
 
Search WWH ::




Custom Search