Java Reference
In-Depth Information
We distinguish between the is-a relationship and the has-a relationship . Is-a repre-
sents inheritance. In an is-a relationship, an object of a subclass can also be treated as an object
of its superclass —e.g., a car is a vehicle. By contrast, has-a represents composition (see
Chapter 8). In a has-a relationship, an object contains as members references to other objects
e.g., a car has a steering wheel (and a car object has a reference to a steering-wheel object).
New classes can inherit from classes in class libraries . Organizations develop their
own class libraries and can take advantage of others available worldwide. Some day, most
new software likely will be constructed from standardized reusable components , just as
automobiles and most computer hardware are constructed today. This will facilitate the
rapid development of more powerful, abundant and economical software.
9.2 Superclasses and Subclasses
Often, an object of one class is an object of another class as well. For example, a CarLoan
is a Loan as are HomeImprovementLoan s and MortgageLoan s. Thus, in Java, class CarLoan
can be said to inherit from class Loan . In this context, class Loan is a superclass and class
CarLoan is a subclass. A CarLoan is a specific type of Loan , but it's incorrect to claim that
every Loan is a CarLoan —the Loan could be any type of loan. Figure 9.1 lists several simple
examples of superclasses and subclasses—superclasses tend to be “more general” and sub-
classes “more specific.”
Superclass
Subclasses
Student
GraduateStudent , UndergraduateStudent
Shape
Circle , Triangle , Rectangle , Sphere , Cube
Loan
CarLoan , HomeImprovementLoan , MortgageLoan
Employee
Faculty , Staff
BankAccount
CheckingAccount , SavingsAccount
Fig. 9.1 | Inheritance examples.
Because every subclass object is an object of its superclass, and one superclass can have
many subclasses, the set of objects represented by a superclass is often larger than the set
of objects represented by any of its subclasses. For example, the superclass Vehicle repre-
sents all vehicles, including cars, trucks, boats, bicycles and so on. By contrast, subclass Car
represents a smaller, more specific subset of vehicles.
University Community Member Hierarchy
Inheritance relationships form treelike hierarchical structures. A superclass exists in a hier-
archical relationship with its subclasses. Let's develop a sample class hierarchy (Fig. 9.2),
also called an inheritance hierarchy . A university community has thousands of members,
including employees, students and alumni. Employees are either faculty or staff members.
Faculty members are either administrators (e.g., deans and department chairpersons) or
teachers. The hierarchy could contain many other classes. For example, students can be
graduate or undergraduate students. Undergraduate students can be freshmen, sopho-
mores, juniors or seniors.
 
 
Search WWH ::




Custom Search