Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Multiple Interface implementation with same methods

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mukesh - 10 Jan 2008 07:37 GMT
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
Marc Gravell - 10 Jan 2008 07:54 GMT
> 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
Mukesh - 10 Jan 2008 08:05 GMT
> > It voilates the principle which says we must provide implementation
> > of
[quoted text clipped - 18 lines]
>
> 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
Jon Skeet [C# MVP] - 10 Jan 2008 08:12 GMT
<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
Peter Duniho - 10 Jan 2008 08:20 GMT
> <snip>
>
[quoted text clipped - 6 lines]
> 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
Jon Skeet [C# MVP] - 10 Jan 2008 08:24 GMT
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.
[quoted text clipped - 12 lines]
> 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
Mukesh - 10 Jan 2008 09:55 GMT
> On Jan 10, 8:20 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
> wrote:
[quoted text clipped - 24 lines]
>
> - 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
Marc Gravell - 10 Jan 2008 08:31 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.