26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9 Object-Oriented <strong>Program</strong>ming 449<br />

libraries and can take advantage of other libraries available worldwide. Someday, most<br />

software might be constructed from standardized reusable components, just as hardware is<br />

often constructed <strong>to</strong>day. This will help meet the challenges of developing the ever more<br />

powerful software we will need in the future.<br />

9.2 Superclasses and Subclasses<br />

Often an object of one class “is an” object of another class as well. A rectangle certainly is<br />

a quadrilateral (as are squares, parallelograms and trapezoids). Thus, class Rectangle<br />

can be said <strong>to</strong> inherit from class Quadrilateral. In this context, class Quadrilateral<br />

is a superclass, and class Rectangle is a subclass. A rectangle is a specific type<br />

of quadrilateral, but it is incorrect <strong>to</strong> claim that a quadrilateral is a rectangle (the quadrilateral<br />

could be a parallelogram). Figure 9.1 shows several simple inheritance examples of superclasses<br />

and potential subclasses.<br />

Inheritance normally produces subclasses with more features than their superclasses,<br />

so the terms superclass and subclass can be confusing. There is another way, however, <strong>to</strong><br />

view these terms that makes perfectly good sense. Because every subclass object “is an”<br />

object of its superclass, and because one superclass can have many subclasses, the set of<br />

objects represented by a superclass is normally larger than the set of objects represented by<br />

any of that superclass’s subclasses. For example, the superclass Vehicle represents in a<br />

generic manner all vehicles, such as cars, trucks, boats, bicycles and so on. <strong>How</strong>ever, subclass<br />

Car represents only a small subset of all the Vehicles in the world.<br />

Inheritance relationships form tree-like hierarchical structures. A superclass exists in<br />

a hierarchical relationship with its subclasses. A class can certainly exist by itself, but it is<br />

when a class is used with the mechanism of inheritance that the class becomes either a<br />

superclass that supplies attributes and behaviors <strong>to</strong> other classes or a subclass that inherits<br />

those attributes and behaviors. Frequently, one class is both a subclass and a superclass.<br />

Superclass Subclasses<br />

Student GraduateStudent<br />

UndergraduateStudent<br />

Shape Circle<br />

Triangle<br />

Rectangle<br />

Loan CarLoan<br />

HomeImprovementLoan<br />

MortgageLoan<br />

Employee FacultyMember<br />

StaffMember<br />

Account CheckingAccount<br />

SavingsAccount<br />

Fig. Fig. 9.1 9.1 Some simple inheritance examples in which the subclass “is a”<br />

superclass.<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!