Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
If a class is implementing two interfaces and both the interfaces is having a method with the same name. Then Is it possible? If yes, then how that class will implement both of those methods?
Posted 20-Aug-12 18:42pm
Edited 20-Aug-12 18:44pm
Kenneth Haugland at 21-Aug-12 0:45am
Overloading... You can have the same function taking different parameters, one could take char the other integers etc.
Sergey Alexandrovich Kryukov at 21-Aug-12 0:49am
No, you don't get the problem. I will answer. --SA
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

I think you need to have a look at the question and my answer in this link,
Understanding interface in C#[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

This is absolutely possible. You need to use explicit interface implementation:
interface IFirst { void SomeMethod(); }
interface ISecond { void SomeMethod(); }
class Implementation : IFirst, ISecond {
    void IFirst.SomeMethod() { /* one implementation */ }
    void ISecond.SomeMethod() { /* another implementation */ }
} //class Implementation
Generally, in many cases I recommend to prefer explicit implementation over implicit one (based on a public method of the same name). The case of method name clash to be resolved demonstrates just one of those benefits.
Practically, this case is not so important. But there is another case where you could have clearly unambiguous syntax which is impossible to express without interfaces — two different indexed property. How to achieve the syntax below?
MyClass myInstance = //...
myInstance[3] = "first case";
myInstance["key"] = "second case";
How? Here is the solution:
interface IAuxIndexer { string this[int index] { get; set; } }
class MyClass {
    string IAuxIndexer.this[int index] { get { /*...*/ } set { /*...*/ } }
    public string this[string index] { get { /*...*/ } set { /*...*/ } }
This way, to have three different indexed property in one class, you need two interfaces, etc.
Zoltán Zörgő at 21-Aug-12 2:55am
My 5! Great!
Sergey Alexandrovich Kryukov at 21-Aug-12 3:04am
Thank you very much, Zoltán. --SA
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Let's say, both interfaces prescribe the same method signature. Overloading won't help you, since there is no difference in name and parameters. Than you can use the fully qualified interface member names. Here are some good samples:[^]
It is up to you if you want to implement all in different ways, or one of them - and that will satisfy all interfacs. But in the first case, you will need to qualify the method on call.
Sergey Alexandrovich Kryukov at 21-Aug-12 1:02am
That is correct, my 5. Please see my answer where I illustrate in on example and show how to solve a more essential problem. --SA
Zoltán Zörgő at 21-Aug-12 2:55am
Thank you. Oh yes, you have posted a really exhaustive answer. I like it! But as I see, deepakdynamite had a simpler problem.
Sergey Alexandrovich Kryukov at 21-Aug-12 14:35pm
I know, I know. Just decided to add a more tricky situation along, something that people ask often... --SA
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Your implementing class will have one method and this will satisfy the requirements of both interfaces. It doesn't matter what type of reference variable it calls, the same method will always be executed.

Check this.[^]
Santhosh Kumar J at 21-Aug-12 0:55am
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

.NET does not support multiple inheritances directly because in .NET, a class cannot inherit from more than one class. .NET supports multiple inheritances through interfaces
for more...
Spam removed.
Pete O'Hanlon at 25-Feb-13 14:12pm
You were warned, before, about spamming the site attempting to get link backs to your site. Your account has been identified as a spammer, and will be removed. I am removing the linkbacks to your site because you are violating the spirit of Code Project.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
0 Guruprasad.K.Basavaraju 460
1 Sergey Alexandrovich Kryukov 336
2 Shai Vashdi 294
3 OriginalGriff 280
4 Abhinav S 160
0 Sergey Alexandrovich Kryukov 9,185
1 OriginalGriff 5,315
2 Peter Leow 4,040
3 Maciej Los 3,535
4 Abhinav S 3,308

Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid