Csci 490 Engr 596 Special Topics Special Projects

  • Slides: 12
Download presentation
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala

Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes

Multiple Inheritance This is a set of slides to accompany chapter 13 of Timothy

Multiple Inheritance This is a set of slides to accompany chapter 13 of Timothy Budd's book An Introduction to Object-Oriented Programming, Second Edition (Addison-Wesley, 1997) Created: 14 August 2004, Revised: 9 March 2010

Orthogonal Classifications Objects often characterized in different ways that are orthogonal to each other

Orthogonal Classifications Objects often characterized in different ways that are orthogonal to each other ØFor example, the instructor is North American Male Professor Computer scientist ØNone of these are proper subsets of each other ØCannot be placed into an inheritance hierarchy 1

Example Complex Numbers Two abstract classifications Ø Magnitude things that can be compared to

Example Complex Numbers Two abstract classifications Ø Magnitude things that can be compared to each other Ø Number things that can perform arithmetic Three specific classes Ø Integer comparable and arithmetic Ø Char comparable but not arithmetic Ø Complex arithmetic but not comparable 2

Solutions Make Number subclass of Magnitude, but redefine comparison operators in class Complex to

Solutions Make Number subclass of Magnitude, but redefine comparison operators in class Complex to print error message Ø Subclassing for limitation Don't use inheritance at all, redefine all operators in all classes Ø Flattening the inheritance tree Use inheritance for some relationships, but simulate others Ø Use Number, but each number implements all relational operators Make Number and Magnitude independent, and have Integer inherit from both Ø Multiple inheritance 3

Inheritance as a Form of Combination Char Magnitude Integer Number Complex 4

Inheritance as a Form of Combination Char Magnitude Integer Number Complex 4

Example Cascading Menus A Menu is structure charged with displaying itself when selected by

Example Cascading Menus A Menu is structure charged with displaying itself when selected by users A Menu maintains collection of Menu. Items Each Menu. Item knows how to respond when selected A cascading menu is both a Menu. Item and Menu 5

Multiple Inheritance Name Ambiguity Problem What happens when same name used in both parent

Multiple Inheritance Name Ambiguity Problem What happens when same name used in both parent classes? A Card. Deck knows how to draw (select) a Card A Graphical. Item knows how to draw (display) an image on screen A Graphical. Card. Deck should be able to draw – but which? 6

Multiple Inheritance Common Ancestors? What happens when parent classes have common root ancestor? Does

Multiple Inheritance Common Ancestors? What happens when parent classes have common root ancestor? Does new object have one or two instances of common ancestor? In. Stream In. Out. Stream 7

Multiple Inheritance in Java supports multiple inheritance of interfaces (subtypes) but not of classes

Multiple Inheritance in Java supports multiple inheritance of interfaces (subtypes) but not of classes (subclasses) interface A {. . . } interface B {. . . } interface AB extends A, B {. . . } interface C {. . . } class X {. . . } class Y extends X implements AB, C {. . . } 8

Multiple Inheritance in Scala supports multiple “mixin” inheritance of traits but not of classes

Multiple Inheritance in Scala supports multiple “mixin” inheritance of traits but not of classes 8

Acknowledgement This work was supported by a grant from Acxiom Corporation titled “The Acxiom

Acknowledgement This work was supported by a grant from Acxiom Corporation titled “The Acxiom Laboratory for Software Architecture and Component Engineering (ALSACE). ” 9