1. Engr 691
Engr 691
Special Topics in Engineering Science
Special Topics in Engineering Science
Software Architecture
Software Architecture
Spring Semester 2004
Spring Semester 2004
Lecture Notes
Lecture Notes
2. Multiple Inheritance
Multiple Inheritance
This Web document was based on a set of slides created by the
This Web document was based on a set of slides created by the
instructor for use in the Fall 1997 and Spring 1999 offerings of the
instructor for use in the Fall 1997 and Spring 1999 offerings of the
object-oriented design and programming course.
object-oriented design and programming course.
That set was, in turn, based on a set of slides created by Timothy
That set was, in turn, based on a set of slides created by Timothy
Budd to supplement chapter 13 of his textbook
Budd to supplement chapter 13 of his textbook
An Introduction to Object-Oriented Programming
An Introduction to Object-Oriented Programming, Second Edition
, Second Edition
(Addison-Wesley, 1997)
(Addison-Wesley, 1997)
3. Orthogonal Classifications
Orthogonal Classifications
Objects often characterized in different ways that are
Objects often characterized in different ways that are
orthogonal to each other
orthogonal to each other
For example, the instructor is
For example, the instructor is
North American
North American
Male
Male
Professor
Professor
Computer scientist
Computer scientist
None of these are proper subsets of each other
None of these are proper subsets of each other
Cannot be placed into an inheritance hierarchy
Cannot be placed into an inheritance hierarchy
4. CS Example: Complex Numbers
CS Example: Complex Numbers
Two abstract classifications:
Two abstract classifications:
Magnitude
Magnitude:
:
things that can be compared to each other
things that can be compared to each other
Number
Number:
:
things that can perform arithmetic
things that can perform arithmetic
Three specific classes:
Three specific classes:
Integer
Integer:
:
comparable and arithmetic
comparable and arithmetic
Char
Char:
:
comparable but not arithmetic
comparable but not arithmetic
Complex
Complex:
:
arithmetic but not comparable
arithmetic but not comparable
5. Solutions
Solutions
1.
1. Make
Make Number
Number subclass of
subclass of Magnitude
Magnitude, but redefine
, but redefine
comparison operators in class
comparison operators in class Complex
Complex to print error
to print error
message –
message – subclassing for limitation
subclassing for limitation
2.
2. Don't use inheritance at all, redefine all operators in all classes
Don't use inheritance at all, redefine all operators in all classes
–
– flattening the inheritance tree
flattening the inheritance tree
3.
3. Use inheritance for some relationships, but simulate others
Use inheritance for some relationships, but simulate others
– use
– use Number
Number, but each number implements all relational
, but each number implements all relational
operators
operators
4.
4. Make
Make Number
Number and
and Magnitude
Magnitude independent, and have
independent, and have
Integer inherit from both –
Integer inherit from both – multiple inheritance
multiple inheritance
6. Inheritance as a Form of
Inheritance as a Form of
Combination
Combination
____ Char
____ Char
____|
____|
|
|
Magnitude <--|
Magnitude <--|
|
|
|____
|____
|____
|____
____ Integer
____ Integer
____|
____|
|
|
Number <--|
Number <--|
|
|
|____
|____
|____ Complex
|____ Complex
7. Another Example:
Another Example:
Cascading Menus
Cascading Menus
A
A Menu
Menu is structure charged with displaying itself when
is structure charged with displaying itself when
selected by users
selected by users
A
A Menu
Menu maintains collection of
maintains collection of MenuItem
MenuItems
s
Each
Each MenuItem
MenuItem knows how to respond when selected
knows how to respond when selected
A
A cascading menu
cascading menu is both a
is both a MenuItem
MenuItem and
and Menu
Menu
8. Multiple Inheritance:
Multiple Inheritance:
Name Ambiguity Problem
Name Ambiguity Problem
What happens when same name used in both
What happens when same name used in both
parent classes?
parent classes?
A
A CardDeck
CardDeck knows how to
knows how to draw
draw (select) a
(select) a Card
Card
A
A GraphicalItem
GraphicalItem knows how to
knows how to draw
draw (display)
(display)
an image on screen
an image on screen
A
A GraphicalCardDeck
GraphicalCardDeck should be able to draw –
should be able to draw –
but which?
but which?
9. Multiple Inheritance:
Multiple Inheritance:
Common Ancestors?
Common Ancestors?
What happens when parent classes have common root ancestor?
What happens when parent classes have common root ancestor?
Does new object have one or two instances of common ancestor?
Does new object have one or two instances of common ancestor?
____ InStream ____
____ InStream ____
____| |____
____| |____
| |
| |
Stream <--| |<-- InOutStream
Stream <--| |<-- InOutStream
|____ ____|
|____ ____|
|____ OutStream ____|
|____ OutStream ____|
10. Multiple Inheritance in Java
Multiple Inheritance in Java
Java supports multiple inheritance of interfaces
Java supports multiple inheritance of interfaces
(subtypes) but not of classes (subclasses)
(subtypes) but not of classes (subclasses)
interface A { ... }
interface A { ... }
interface B { ... }
interface B { ... }
interface AB extends A, B { ... }
interface AB extends A, B { ... }
interface C { ... }
interface C { ... }
class X { ... }
class X { ... }
class Y extends X implements AB, C { ... }
class Y extends X implements AB, C { ... }