DOC PREVIEW
UA CSC 520 - OO Languages - Multiple Inheritance

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

UA CSC 520 - OO Languages - Multiple Inheritance

Documents in this Course
Handout

Handout

13 pages

Semantics

Semantics

15 pages

Haskell

Haskell

15 pages

Recursion

Recursion

18 pages

Semantics

Semantics

12 pages

Scheme

Scheme

32 pages

Syllabus

Syllabus

40 pages

Haskell

Haskell

17 pages

Scheme

Scheme

27 pages

Scheme

Scheme

9 pages

TypeS

TypeS

13 pages

Scheme

Scheme

27 pages

Syllabus

Syllabus

10 pages

Types

Types

16 pages

FORTRAN

FORTRAN

10 pages

Load more
Download OO Languages - Multiple Inheritance
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view OO Languages - Multiple Inheritance and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view OO Languages - Multiple Inheritance 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?