all groups > dotnet academic > december 2004 >
You're in the

dotnet academic

group:

interfaces and diamond inheritance


interfaces and diamond inheritance Gilles
12/23/2004 7:01:03 AM
dotnet academic:
When a class implements several interfaces, it may occur that a function with
the same name exists in two interfaces. A solution is provided: explicit
implementation of this method, that is, using the qualified name.

But what occurs when a class implements two interfaces deriving from a
common interface?

interface A {void Close();}
interface B:A{...}
interface C:A{...}
class D: B,C
{
void B.Close(){...} -> error
void B.A.Close(){...} -> error
void Close(){...} -> correct
void A.Close(){...} -> correct
}

It seems there is no way to define a different Close function for each
interface.
Only one common Close function could be defined, even if a different
behavior is desired, depending on the cast.

Re: interfaces and diamond inheritance Mattias Sjögren
12/28/2004 4:18:11 PM

[quoted text, click to view]

Right. If there was, which implementation would be called by this
code?

A a = new D();
a.Close();



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Re: interfaces and diamond inheritance Gilles
1/3/2005 2:43:01 AM
Well, I'd say it would be selected by an explicit casting, as it occurs with
explicit implementation.

That is, a function implemented with an explicit reference to the interface
(a "fully decorated name"?) can be called only when the instance is cast as
this interface:

interface A{void Close();...}
interface B{void Close();...}
class C: A, B
{
void A.Close(){...}
void B.Close(){...}
}

C objC = new C();
A objA = (A) objC;
B objB = (B) objC;

objC.Close() => error!
objA.Close() => Close fct of interface A
objB.Close() => Close fct of interface B

Unfortunately, this mechanism doesn't work when Close fct belongs to an
AddThis Social Bookmark Button