1 / 10

Multiple Inheritance

Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes. Multiple Inheritance.

julianp
Download Presentation

Multiple Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Engr 691Special Topics in Engineering ScienceSoftware ArchitectureSpring Semester 2004Lecture Notes

  2. Multiple Inheritance This Web document was based on a set of slides created by the instructor for use in the Fall 1997 and Spring 1999 offerings of the object-oriented design and programming course. That set was, in turn, based on a set of slides created by Timothy Budd to supplement chapter 13 of his textbook An Introduction to Object-Oriented Programming, Second Edition (Addison-Wesley, 1997)

  3. 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

  4. CS 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

  5. 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

  6. Inheritance as a Form of Combination ____ Char ____| | Magnitude <--| | |____ |____ ____ Integer ____| | Number <--| | |____ |____ Complex

  7. Another Example:Cascading Menus • A Menu is structure charged with displaying itself when selected by users • A Menu maintains collection of MenuItems • Each MenuItem knows how to respond when selected • A cascading menu is both a MenuItem and Menu

  8. Multiple Inheritance: Name Ambiguity Problem • What happens when same name used in both parent classes? • A CardDeck knows how to draw (select) a Card • A GraphicalItem knows how to draw (display) an image on screen • A GraphicalCardDeck should be able to draw – but which?

  9. Multiple Inheritance: Common Ancestors? • What happens when parent classes have common root ancestor? • Does new object have one or two instances of common ancestor? ____ InStream ____ ____| |____ | | Stream <--| |<-- InOutStream |____ ____| |____ OutStream ____|

  10. Multiple Inheritance in Java 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 { ... }

More Related