CSci 581: Obj.-Oriented Design & Programming
Spring Semester 1999
Lecture Notes


Multiple Inheritance

This Web document was based on a set of "slides" created by the instructor for use in the Fall 1997 offering 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).


Orthogonal Classifications

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

For example, the instructor is

None of these are proper subsets of each other

Cannot be placed into an inheritance hierarchy


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


Solutions

  1. Make Number subclass of Magnitude, but redefine comparison operators in class Complex to print error message

    -- subclassing for limitation

  2. Don't use inheritance at all -- redefine all operators in all classes

    -- flattening the inheritance tree

  3. Use inheritance for some relationships, but simulate others -- use Number, but have each number implement all relational operators

  4. Make Number and Magnitude independent, and have Integer inherit from both

    -- multiple inheritance


Inheritance as a Form of Combination

                   ____ Char
              ____|
             |
Magnitude <--|
             |
             |____
                  |____
                   ____ Integer
              ____|
             |
   Number <--|
             |
             |____
                  |____ Complex


Another Example -- Cascading Menus


Multiple Inheritance: Name Ambiguity Problem

What happens when same name is used in both parent classes?


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 ____|


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 { ... }


UP to CSCI 581 Lecture Notes root document?


Copyright © 1999, H. Conrad Cunningham
Last modified: Tue Apr 20 09:17:23 CDT