![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Deciding between a subclasssuperclass relation and a partwhole relation
is not always straightforward, however. For example, you may decide to
model a piano as an instrument that has a keyboard, or you may decide to
model a piano as a keyboard instrument. If you follow the has-a rule, you
implement the Piano
class as a subclass of the Instrument
class; if you follow the is-a rule, you implement the Piano
class as
a subclass of the KeyboardInstrument
class, and you implement the
KeyboardInstrument
class as a subclass of the Instrument
class.
The rule that you should follow is the one that seems to make the most
sense in light of those aspects of the real world that you are modeling.
If your program is to deal with many types of keyboard instruments, and if
the types share behavior, then following the is-a rule, rather than the
has-a rule, is the better choice, and you should define a
KeyboardInstrument
class