Cook Computing

Multiple Inheritance and Envision Eiffel

October 2, 2002 Written by Charles Cook

How does Eiffel support Multiple Inheritance in the .NET environment? After all, the CLR does not support MI for classes. But it does support MI for interfaces and this is the key to the solution.

As mentioned in an earlier entry here, each Eiffel class is represented by an interface and two classes when compiled into a .NET assembly. The interface is the type used to represent the class in client code, the Create class provides a static method to instantiate instances of the class, and the Impl class is the implementation of the class.

Suppose we have an Eiffel class called CHILD inheriting from classes PARENT1 and PARENT2. In the generated .NET assembly there will be three interfaces: Parent1, Parent2, and Child which derives from both of the parent interfaces.

Once an instance of Child has been acquired, it can be cast to Parent1 or Parent2 which simulates upcasting to the base class:

Child child = Create.Child.Make();
Parent1 p1 = (Parent1)child;
Parent2 p2 = (Parent2)child;

This works because class Impl.Child implements interface Child, which implies the class, or one of its base classes, implements interfaces Parent1 and Parent2. The obvious question which follows from this is: if MI is not supported in .NET, how can methods in the parent classes be invoked? Say PARENT1 has feature Foo and PARENT2 has feature Bar, how does the following work:

Child  child  = Create.Child.Make(); 
Parent1 p1 = (Parent1)child;
Parent2 p2 = (Parent2)child;

The answer is that Impl.Child implements all the methods of the Parent1 and Parent2 interfaces so avoiding the need to call the methods in the base classes. The methods are of course also implemented in the base classes Impl.Parent1 and Impl.Parent2 so there is a some duplication of code within the assembly. Impl.Child itself only derives from Object and so has no relationship with the parent Impl classes.

Out of curiosity I tried creating an Eiffel class which inherited from two external .NET classes implemented in C#. It failed to compile with this error message:

Error: class inherits multiply from classes that are .NET external classes or that inherit from .NET external classes.