Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^330) | Inheritance, Polymorphism, and Scope
These CRC cards might originate in the brainstorming phase, or they could re-
sult from a series of scenarios. No matter how they arise, it is quite obvious that
they have several responsibilities in common: “Know its name”, “Know its city”,
“Know its state”, and “Know its ZIP code”. We can save ourselves duplicate effort
by defining a superclass, Address, that has the common responsibilities. In addition,
two of the classes share the “Know its company” responsibility. We can thus de-
fine a subclassof Address called Company Address that becomes the superclass of
these two. The third class directly extends Address with its specific responsibili-
ties. Here are the revised CRC cards:
Class Name: BoxAddress Superclass: Subclasses:
Responsibilities Collaborations
Create itself (name, company, box, city,
state, ZIP code)
Name
Know its name Name
Know its company
Know its box
None
Know its city
Know its state
None
Know its ZIP code
None
None
None
return String
return String
return String
return String
return String
return String
Superclass A class that is ex-
tended by one or more derived
classes (its subclasses)
Subclass A class that is
derived from another class (its
superclass)
T
E
A
M
F
L
Y
Team-Fly®

Free download pdf