Multiple InheritanceMultiple Inheritanceldots Multiple Inheritanceldots Multiple Inheritanceldots Multiple Inheritanceldots Multiple Inheritanceldots Multiple Inheritanceldots Multiple Inheritanceldots Multiple Inheritanceldots Multiple Inheritanceldots520—Spring 2005—47CSc 520Principles of ProgrammingLanguages47: OO Languages — Multiple InheritanceChristian [email protected] of Computer ScienceUniversity of ArizonaCopyrightc 2005 Christian Collberg[1]520—Spring 2005—47Multiple InheritanceIn some languages (C++, Eiffel) a class can have morethan one superclass.class Person { Name : STRING; }class Student extends Person {Advisor : Teacher;}class Teacher extends Person {Salary : INTEGER;method Rich () : BOOLEAN;return Salary > 50000;}class Tutor extends Student, Teacher {Boss : Teacher;}[2]520—Spring 2005—47Multiple Inheritance...class Teacher extends Person {Salary : INTEGER;method Rich () : BOOLEAN;return Salary > 50000;}Rich() should translate into:PROCEDURE Rich (SELF : Teacher) : BOOLEAN;RETURN SELFˆ.Salary > 50000;[3]520—Spring 2005—47Multiple Inheritance...We’d like to be able to call m.Rich() for any Teacherobject, including a Tutor:PROCEDURE Rich (SELF : Teacher) : BOOLEAN;RETURN SELFˆ.Salary > 50000;Teacher Knuth =new Teacher;Tutor Lucy =new Tutor;boolean k = Knuth.Rich()boolean l = Lucy.Rich()In order for this to work, the Salary field in a Tutorrecord must be at the same offset as the Salary fieldin theTeacher record.[4]520—Spring 2005—47Multiple Inheritance...But, if our record layout uses simple concatenation ofparent classes (like with single inheritance), we get:Person0:Name:String0:Name:String4:Salary:INTTeacher0:Name:String4:Advisor:TeacherStudentTutor8:Salary:INT0:Name:String4:Advisor:TeacherFrom StudentFrom TeacherThe Salary field in a Teacher record is at offset 4, buttheSalary field in the Tutor record is at offset 8.[5]520—Spring 2005—47Multiple Inheritance...An inefficient implementation might do:PROCEDURE Rich (SELF : Teacher) : BOOLEAN;RETURN IF ISTYPE(SELF,Teacher)THEN (SELF-4)ˆ>50000 ELSE (SELF+8)ˆ>50000;Or we could insert extra space to align the fields properly:Person0:Name:String0:Name:String4:Advisor:Teacher8:Salary:INTTutor0:Name:String4:Advisor:TeacherStudent0:Name:String8:Salary:INTTeacher4:Wasted:4−bytes[6]520—Spring 2005—47Multiple Inheritance...With multi-directional layouts, we place variables at bothpositive and negative offsets:0:Name:String4:Advisor:TeacherStudent−4:Salary:INT0:Name:String4:Advisor:TeacherTutorPerson0:Name:StringTeacher0:Name:String−4:Salary:INT[7]520—Spring 2005—47Multiple Inheritance...0:Name:String4:Advisor:TeacherStudent−4:Salary:INT0:Name:String4:Advisor:TeacherTutorPerson0:Name:StringTeacher0:Name:String−4:Salary:INTThe Salary-field is always at the same offset,regardless of what type of object:PROCEDURE Rich (SELF : Teacher) : BOOLEAN;RETURN (SELF-4)ˆ>50000;[8]520—Spring 2005—47Multiple Inheritance...How does the language deal with the same fieldinherited through more than one path? ATutorinherits Name twice, once from Student and once fromTeacher:class Person { Name : STRING; }class Student extends Person {· · ·}class Teacher extends Person {· · ·}class Tutor extends Student,Teacher {· · ·}Should Tutor have one or two copies of Name?In Trellis/Owl you always get just one copy of Name.In C++ you can choose. If you declare a superclassvirtual,Tutor only gets one copy of Name, otherwisetwo.[9]520—Spring 2005—47Multiple Inheritance...How does the language deal with differentfields/methods with the same type/signature inheritedfrom different classes?class Student {Name : STRING; · · · }class Teacher {Name : STRING; · · · }class Tutor extends Student,Teacher {· · ·}Tutor T = new Tutor();T.Name = "Knuth"; /* Which Name? */[10]520—Spring 2005—47Multiple Inheritance...class Student {Name : STRING; · · · }class Teacher {Name : STRING; · · · }class Tutor extends Student,Teacher {· · ·}Tutor T = new Tutor();T.Name = "Knuth"; /* Which Name? */In Eiffel, the programmer has to rename fields untilthere are no more conflicts, using arename clause:class Tutor extends Student,Teacherrename Name⇒TName {· · ·}In C++, conflicts are resolved when the field/method isused:Tutor T = new Tutor();Teacher::T.Name =
View Full Document