Java Reference
In-Depth Information
Object-oriented programming provides us with a feature called inheritance that
increases our ability to reuse code by allowing one class to be an extension of another.
Inheritance also allows us to write programs with hierarchies of related object types.
Nonprogramming Hierarchies
In order to use inheritance, you'll want to identify similarities between different
objects and classes in your programs. Let's start by looking at a nonprogramming
example: a hierarchy of employees at a company.
Imagine a large law firm that hires several classes of employees: lawyers, general
secretaries, legal secretaries, and marketers. The company has a number of employee
rules about vacation and sick days, medical benefits, harassment regulations, and so
on. Each subdivision of the company also has a few of its own rules; for example,
lawyers may use a different form to ask for vacation leave than do secretaries.
Suppose that all the employees attend a common orientation where they learn the
general rules. Each employee receives a 20-page manual of these rules to read. A
mixed group of employees could attend the orientation together: Lawyers, secre-
taries, and marketers all might sit in the same orientation group.
Afterward, the employees go to their subdivisions and receive secondary, smaller
orientations covering any rules specific to those divisions. Each employee receives a
smaller manual, two or three pages in length, covering that subdivision's specific
rules. Some rules are added to those in the general 20-page manual, and a few are
replaced. For example, one class of employees may get three weeks of vacation
instead of two, and one class may use a pink form to apply for time off rather than
the yellow form listed in the 20-page manual. Each class has its own submanual with
unique contents as shown in Figure 9.1.
Employee
20-page manual
Lawyer
2-page manual
Secretary
1-page manual
Marketer
3-page manual
LegalSecretary
1-page manual
Figure 9.1
A hierarchy of employee manuals
 
Search WWH ::




Custom Search