Professional Documents
Culture Documents
Generalizations
Multiple Inheritance
(finishing up Class Design)
1
Class Design Steps
2
Generalization
Is a notational convenience
3
Define Generalizations
Purpose
Identify areas of reuse
Refine existing inheritance hierarchies so that
they can be implemented efficiently
Things to look for:
Abstract classes vs. concrete classes
Multiple inheritance problems
Generalization vs. Aggregation
Generalization to support implementation reuse
4
Review: Generalization
Is a ‘relationship where one class shares the structure
and/or behavior of one or more classes
“Is-a-kind of” relationship ancestor
Account
Should always be able to say that balance
your derived / child class ‘is a kind of’ parent class name
Superclass number
Can us terms ‘ancestor’ and (parent)
‘descendent’ instead of Withdraw()
super-class and subclass. CreateStatement()
Generalization
In analysis, generalization is
Relationship
used sparingly to model
shared behavioral semantics only.
(generalization must pass ‘is a’ test).
Checking Savings
Subclasses
GetInterest()
6
Abstract and Concrete Classes
Abstract classes cannot have any objects
Exist only for other classes to inherit from it
Concrete classes are used to instantiate objects
Abstract class
Animal
Discriminator {abstract}
Abstract
An operation can also be tagged as
abstract in UML. talk () {abstract} operation
Meaning: no implementation exists for
the operation in the class where it is There can be no direct
Communication
specified. instances of Animal
multiple
inheritance
getColor getColor
FlyingThing Animal
Bird
Bird
Window
WindowWithScrollbar Scrollbar
1 1
12
Generalization: Share Common
Properties and Behavior
Follows the is-a style of programming
Class substitutability List
insertTop (Item)
Animal insertBottom (Item)
removeTop ()
talk () removeBottom ()
insert (Item,
position)
???
Lion Tiger
talk () talk ()
Stack
Lion Tiger
talk () talk ()
Stack
Lion Tiger
talk () talk ()
Stack
The classes on the left-hand side of the diagram do follow the is-a style of programming:
A Lion is-an Animal and a Tiger is-an animal.
The classes on the right-hand side of the diagram do NOT follow the is-a style of
programming: a Stack is not a List.
A Stack needs some of the behavior of a List but not all of the behavior.
16
Generalization: Share Implementation-Factoring
Factoring is useful if there are some services provided by one class that
you want to leverage in the implementation of another class.
When you factor, extract the functionality you want to reuse and
inherit it from the new base class.
SequentialContainer
List
insertTop (Item)
insertTop (Item) removeTop ()
insertBottom (Item)
removeTop ()
removeBottom ()
insert (Item, position)
List Stack
insertBottom (Item)
removeBottom ()
insert (Item, position)
20
Checkpoints: Relationships
Are the role names descriptive?
Define roles?? Remember?
22