Title: Multiple Inheritance
1Multiple Inheritance
- Fall 2005 OOPD
- John Anthony
2The Problem
Male
North American
Parent
Professor
Author
3The (Conceptual) Solution
By breaking down the hierarchy, each abstract
type can be combined to create valid is-a
Relationships.
4Multiple Inheritance
- An Author is-a Male and is-a North American
and. - An Author can be viewed as-a Male and as-a
North American and.
5Consider
Professor
Student
getDepartment ()
getDepartment ()
Name Ambiguity Which getDepartment() is Student
going to inherit?
TeacherAssistant
getDepartment ()
6Resolving Name Ambiguity
Force the client of the child class to resolve
the ambiguity
- TeacherAssistant assistant
- assistant-gtProfessorgetDepartment()
- assistant-gtStudentgetDepartment()
7Resolving Name Ambiguity
If the signatures are different (and overloading
supported) then child class can simply include
both methods.
8Resolving Name Ambiguity
If signatures are similar or if overloading not
allowed, then programmer may override one method
and rename another.
- Class TeachingAssistant public Professor,
public Student - public
- virtual Department getTeachingDepartment()
- return ProfessorgetDepartment()
-
- virtual Department getDepartment()
- return StudentgetDepartment()
-
What about polymorphism?
Professor p new TeachAssistant() p-gtgetDepart
ment() //returns the Department the TA is
studying in rather than teaching in.
9Resolving Name Ambiguity
A C idiom to addressing name ambiguity.
Professor
Student
getDepartment ()
getDepartment ()
ProfessorParent
StudentParent
getDepartment ()
getDepartment ()
getMajorDepartment()
getTeachingDepartment()
TeacherAssistant
getTeachingDepartment()
getMajorDepartment()
10Idiom Cont.
- Class ProfessorParent public Professor
- public
- virtual Department getDepartment()
- return getTeachingDepartment()
-
- virtual Department getTeachingDepartment()
- return Professor getDepartment()
-
11Resolving Name Ambiguity
- TeachingAssistant ta new TeachingAssistant()
- Professor prof ta
- Student student ta
- prof-gtgetDepartment() //OK, will execute
getTeachingDepartment - Student-gtgetDepartment() //OK, will execute
getMajorDepartment - ta-gtgetDepartment() //compiler error, ambiguous
invocation
12Redefinition in Eiffel
- cClass TeachingAssistant
- inherit
- Professor
- rename
- getDepartment as getTeachingDepartment
- end
- Student
- rename
- getDepartment as getMajorDepartment
- end
13Common Ancestors
A
A
A
B
C
B
C
D
D
14Common Ancestors
Person
String title
the diamond of death
Professor
Student
String title
String title
TeacherAssistant
Should there be two titles or one?