Multiple Interface implementation with same methods

M

Mukesh

Hi,
Consider a situation where a class (Say C1) is implementing two
interfaces (I1, I2). Both the interface contains a method (M1) with
the same signature.

class C1:I1, I2
{
//implementation of M1 only once
}
I wanna understand why we need only one implementation of M1. It
voilates the principle which says we must provide implementation of
every method for all the interfaces.
In any case which interface's M1 is being implemented here i mean I1
or I2?
Thanks
Mukesh
 
M

Marc Gravell

It voilates the principle which says we must provide implementation
of
every method for all the interfaces.
No it doesn't... we *have* provided an implementation for each... they
just aren't *different* implementations.

Both I1 and I2 will pick up the implicit interface implementation from
M1 via the public M1 method, as per the language spec. You can,
however, opt to make either (or both) implementations explicit:

class C1 : I1, I2 {
public void M1() {...}
void I2.M1() {...}
}

Here there is an explicit imlementation for I2.M1(); however, when
checking I1, there is no explicit implementation, so it checks for
matching public methods - which it finds (an implicit implementation).

Marc
 
M

Mukesh

No it doesn't... we *have* provided an implementation for each... they
just aren't *different* implementations.

Both I1 and I2 will pick up the implicit interface implementation from
M1 via the public M1 method, as per the language spec. You can,
however, opt to make either (or both) implementations explicit:

class C1 : I1, I2 {
  public void M1() {...}
  void I2.M1() {...}

}

Here there is an explicit imlementation for I2.M1(); however, when
checking I1, there is no explicit implementation, so it checks for
matching public methods - which it finds (an implicit implementation).

Marc
Hi, Marc
That's fine. Now if two interfaces accidentally have a method with
same signature but we want different functional implementaion of both
the methods. Can we use some different names (say M2 and M3) while
implementing I1.M1() and I2.M1() so that its more logical for the
users of class C1.
Thanks
Mukesh
 
J

Jon Skeet [C# MVP]

That's fine. Now if two interfaces accidentally have a method with
same signature but we want different functional implementaion of both
the methods. Can we use some different names (say M2 and M3) while
implementing I1.M1() and I2.M1() so that its more logical for the
users of class C1.

No. Users of the class who want to use it "as" a particular interface
need to cast to that interface.

Jon
 
P

Peter Duniho

No. Users of the class who want to use it "as" a particular interface
need to cast to that interface.

Maybe I didn't understand the question correctly, but I think he's asking
whether two public methods M2 and M3 that would be used outside the
context of the interface can be used as the implementation for each of the
interface methods as well.

If so, then the answer is basically yes. I mean, you can't alias the
method names or anything like that, but you can just have the interface
methods call some other method directly. So I1.M1() could call M2 and
I2.M1() could call M3.

You're right that when the interface methods are declared explicitly, a
cast is needed to get at them. But I didn't think that's what Mukesh was
asking.

Pete
 
J

Jon Skeet [C# MVP]

Maybe I didn't understand the question correctly, but I think he's asking
whether two public methods M2 and M3 that would be used outside the
context of the interface can be used as the implementation for each of the
interface methods as well.

If so, then the answer is basically yes. I mean, you can't alias the
method names or anything like that, but you can just have the interface
methods call some other method directly. So I1.M1() could call M2 and
I2.M1() could call M3.

You're right that when the interface methods are declared explicitly, a
cast is needed to get at them. But I didn't think that's what Mukesh was
asking.

I think he was asking about the aliasing side. I *believe* that VB.NET
actually lets you do that. But yes, explicit interface implementation
which calls a public/internal method with a more descriptive name is
an easy way of getting round the limitation.

Jon
 
M

Marc Gravell

Aside (for the OP's benefit): what you describe is supported at the IL
level (and VB allows this usage natively), but it isn't possible via
C#. But as Peter observes, it is trivial to just forward the methods
from the explicit implementations.

Marc
 
M

Mukesh

I think he was asking about the aliasing side. I *believe* that VB.NET
actually lets you do that. But yes, explicit interface implementation
which calls a public/internal method with a more descriptive name is
an easy way of getting round the limitation.

Jon- Hide quoted text -

- Show quoted text -

Hi Jon,
You are right. Actually method names for a class should signify the
functionality it provides. Since the method name for the two methods
is same that may be a bit ambiguous for the user using that class. So
I wanted to refine the names in the context of that class to reflect
the implementation functionality.
Thanks for the solution.
Mukesh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top