473,413 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Multiple Interface implementation with same methods

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
Jan 10 '08 #1
7 10293
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
Jan 10 '08 #2
On Jan 10, 12:55*pm, "Marc Gravell" <marc.grav...@gmail.comwrote:
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
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
Jan 10 '08 #3
On Jan 10, 8:05 am, Mukesh <cmukes...@gmail.comwrote:

<snip>
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
Jan 10 '08 #4
On Thu, 10 Jan 2008 00:12:39 -0800, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
On Jan 10, 8:05 am, Mukesh <cmukes...@gmail.comwrote:

<snip>
>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.
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
Jan 10 '08 #5
On Jan 10, 8:20 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
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.
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
Jan 10 '08 #6
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
Jan 10 '08 #7
On Jan 10, 1:24*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jan 10, 8:20 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:


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.

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
Jan 10 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
8
by: Gaetan | last post by:
hi i have 2 classes A1 and A2 implementing a problem with 2 different ways i also have 2 other classes X1 and X2 implementing an other problem i need classes that provide A1+X1 methods,...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
29
by: MAHESH MANDHARE | last post by:
Hi , Can Anyone explain me exactly why multiple inheritance not used in java and c# thanks, Mahesh -- Have A Good Day, Mahesh, Maheshmandhare@yahoo.co.in
6
by: Ricky W. Hunt | last post by:
It's dawning on my a lot of my problems with VB.NET is I'm still approaching it in the same way I've programmed since the late 70's. I've always been very structured, flow-charted everything, used...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
3
by: ernesto | last post by:
Hi everybody I have the following class declarations: class Interface { public: virtual char* getName() const = 0; }; class BaseClass : public Interface {
6
by: Joseph Geretz | last post by:
I have the following class which I am serializing and passing back and forth between my Web Service application and the client. public class Token : SoapHeader { public string SID; public...
11
by: John | last post by:
Hi All, Although C# has Generics, it still does not support the generic programming paradigm. Multiple inheritance is required to support real generic programming. Here is a simple design pattern...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.