next up previous contents
Next: The Java class hierarchy Up: Subclasses and inheritance Previous: Inheritance polymorphism   Contents

Multiple inheritance

Is it possible for a class to extend more than one class. For instance, could we have the following?

  class C1{
    ...
  }

  class C2{
    ...
  }

  class C3 extends C1, C2{
    ...
  }

The problem is that what C3 inherits from C1 might conflict with what it inherits from C2. Suppose we have a method f(..) in both C1 and C2 as follows:

  class C1{
    ...
    public int f(int i){ ... }
    ..
  }

  class C2{
    ...
    public int f(int i){ ... }
    ..
  }

  class C3 extends C1, C2{
    ...     // no definition of "public int f(int i)" in C3
  }

Now, if we create an object of type C3 and invoke f(..), it is not clear whether we should use the definition of f(..) from C1 or C2.

One possibility is to say that C3 can extend C1 and C2 provided it explicitly redefines all conflicting methods. Another is to say that C3 can extend C1 and C2 provided there are no conflicting methods. The latter option is available in C++.



Madhavan Mukund 2004-04-29