Click here to Skip to main content
14,870,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 20-Aug-12 18:44pm
v2
Comments
Kenneth Haugland 21-Aug-12 0:45am
   
Overloading... You can have the same function taking different parameters, one could take char the other integers etc.
   
No, you don't get the problem. I will answer.
--SA

I think you need to have a look at the question and my answer in this link,

Understanding interface in C#[^]
   
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: http://stackoverflow.com/questions/7080861/c-sharp-interface-method-ambiguity[^]
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.
   
v4
Comments
   
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ő 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 21-Aug-12 14:35pm
   
I know, I know. Just decided to add a more tricky situation along, something that people ask often...
--SA
This is absolutely possible. You need to use explicit interface implementation:
C#
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?
C#
MyClass myInstance = //...
myInstance[3] = "first case";
myInstance["key"] = "second case";


How? Here is the solution:
C#
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.

—SA
   
v5
Comments
Zoltán Zörgő 21-Aug-12 2:55am
   
My 5! Great!
   
Thank you very much, Zoltán.
--SA
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.
http://stackoverflow.com/questions/4638218/class-implementing-two-interfaces-which-define-the-same-method[^]
   
Comments
Santhosh Kumar Jayaraman 21-Aug-12 0:55am
   
:)
.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.
   
v2
Comments
Pete O'Hanlon 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)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900