140 likes | 253 Vues
This document by John J. Anthony explores the complexities of multiple inheritance in object-oriented programming, specifically focusing on name ambiguity. It highlights how abstract types can be effectively managed to establish valid "is-a" relationships among classes like Author, Professor, and Student. By breaking down the hierarchy, the issue of ambiguity when inheriting methods, such as getDepartment(), can be resolved. Practical examples in C++ illustrate methods to prevent ambiguity and ensure that the correct method is invoked, alongside discussions on polymorphism and method overriding.
E N D
Multiple Inheritance Fall 2005 OOPD John Anthony
The Problem Male North American Parent Professor Author John J. Anthony
Male North American Parent Professor Author The (Conceptual) Solution By breaking down the hierarchy, each abstract type can be combined to create valid “is-a” Relationships. John J. Anthony
Parent Professor North American Male Author Multiple 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…. John J. Anthony
Consider… Professor Student getDepartment (…) getDepartment (…) Name Ambiguity Which getDepartment(…) is Student going to inherit? TeacherAssistant getDepartment (…) John J. Anthony
Resolving Name Ambiguity TeacherAssistant assistant; assistant->Professor::getDepartment(); assistant->Student::getDepartment(); Force the client of the child class to resolve the ambiguity… John J. Anthony
Professor Student getDepartment (int majorCode) getDepartment () TeachingAssistant getDepartment () getDepartment (int majorCode) Resolving Name Ambiguity If the signatures are different (and overloading supported) then child class can simply include both methods. John J. Anthony
Resolving Name Ambiguity Class TeachingAssistant : public Professor, public Student { public: virtual Department * getTeachingDepartment() { return Professor::getDepartment(); } virtual Department * getDepartment() { return Student::getDepartment(); } } If signatures are similar or if overloading not allowed, then programmer may override one method and rename another. What about polymorphism? Professor * p = new TeachAssistant(); p->getDepartment(); //returns the Department the TA is studying in rather than teaching in. John J. Anthony
Resolving Name Ambiguity A C++ idiom to addressing name ambiguity…. Professor Student getDepartment () getDepartment () ProfessorParent StudentParent getDepartment () getDepartment () getMajorDepartment() getTeachingDepartment() TeacherAssistant getTeachingDepartment() getMajorDepartment() John J. Anthony
Professor getDepartment () ProfessorParent getDepartment () getTeachingDepartment() Idiom Con’t. Class ProfessorParent : public Professor { public: virtual Department * getDepartment() { return getTeachingDepartment(); } virtual Department * getTeachingDepartment() { return Professor ::getDepartment(); } } John J. Anthony
Resolving Name Ambiguity TeachingAssistant * ta = new TeachingAssistant(); Professor *prof = ta; Student *student = ta; prof->getDepartment(); //OK, will execute getTeachingDepartment Student->getDepartment(); //OK, will execute getMajorDepartment ta->getDepartment(); //compiler error, ambiguous invocation John J. Anthony
Redefinition in Eiffel cClass TeachingAssistant inherit Professor rename getDepartment as getTeachingDepartment end Student rename getDepartment as getMajorDepartment end John J. Anthony
Common Ancestors A A A B C B C D D John J. Anthony
Common Ancestors Person String *title; …the diamond of death Professor Student String *title; String *title; TeacherAssistant Should there be two titles or one? John J. Anthony