Sunday, May 11, 2008

Implementing Two interface having the same method signature in the same class

There can be scenario when we would have two interface with the same method name and same type of arguments, same number of arguments and even the return type can be same and we need to implement both the interface in the same class. How to implement the interface in our class? Most of you will be thinking whats so tough in this, just implement the interface and the method and move on. If the signature of the methods in the interface were different then there would have been no problem but here the signature of the methods in two different interface are same and both the interfaces are gonna be implemented in the same class. The two interface are as below.
public interface IA
{
string PrintName();
}
public interface IB
{
string PrintName();
}

From the above code we can infer that we have two interface with names IA and IB and both have a single method named “PrintName”. The signature of both the methods are same and we need to implement the interfaces in our class say “A”. One way of impelementing the interface is as shown below i.e. just having a “public” implementation of the interface method only once.
public class A : IA, IB
{
public A()
{
}
public string PrintName()
{
return this.GetType().Name;
}
}

The above implementation has got a limitation i.e the method “PrintName” is considered to be a common method for all i.e commong method for the class, and for the interfaces IA and IB. If you are writing a code shown below
A a = new A();
IA ia = new A();
IB ib = new A();
Console.WriteLine(a.PrintName());
Console.WriteLine(ia.PrintName());
Console.WriteLine(ib.PrintName());

all the calls to method “PrintName” will give you the same result, the name of the class i.e. “A”. This is because all call to the method goes to the same definition. Now if you want to give different implementation to the methods in interface IA and IB, what will you do? Its also simple, just have two impelementation of the same method and prefix the method names with the interface name as shown below.
public class A : IA, IB
{
public A()
{
}
string IA.PrintName()
{
return “IA PrintName Method”;
}
string IB.PrintName()
{
return “IB PrintName Method”;
}
}

Now the below code will give you different output.
IA ia = new A();
IB ib = new A();
Console.WriteLine(ia.PrintName());
// Will print "IA PrintName Method"
Console.WriteLine(ib.PrintName());// Will print "IB PrintName Method"
So this is how two interfaces having the same method signature can be given different implementation in the same class.

50 comments:

  1. Thank you for such simple and clear solution

    ReplyDelete
  2. Nicely explained the concept.Thank You

    ReplyDelete
  3. The overall topic was good. However, it has missed out one thing.
    consider, A a = new A();

    Then there is no PrintName method for the object a.
    a.PrintName(); // Compiler Error

    ReplyDelete
  4. Hi Mukerji,
    Thanks for the comment. As you have pointed out in your comment the compiler will throw an error saying "so and so method could not be found".The reason why this was not included as a sample code was that if you are using any version of Visual Studio, which 100% of .net developers are, the intellisense will not pop the interface method as one of the methods of the class unless you cast it to the respective interface. Thanks for pointing this out and with this discussion we have closed the missing link as well. :)

    ReplyDelete
  5. very good yaar keep it up vikas.sharma2009@live.com

    ReplyDelete
  6. Thanks for this clear solution.

    ReplyDelete
  7. Excellent example...

    ReplyDelete
  8. Very nice explained...

    ReplyDelete
  9. thanks for nice example

    ReplyDelete
  10. very nice explanation

    ReplyDelete
  11. very nice explanation ....

    ReplyDelete
  12. Simple but very good explaination

    ReplyDelete
  13. Thanks guys for the positive feedback.

    ReplyDelete
  14. totally wrong ,it is not possible in .net
    ,which same method name in two interface

    ReplyDelete
  15. Hi,
    Whatever is explained in the article is very much possible in .NET. All the code pasted here have been tested thoroughly. If you could highlight the problem you are facing it will be helpful for me to help you out with your problem.

    ReplyDelete
  16. Very nice explained.Thanks

    ReplyDelete
  17. but what if in both interface I am defining same method with different return types,it will give CTE.

    interface i1{
    public void foo();
    }
    interface i2{
    public int foo();
    }

    ReplyDelete
    Replies
    1. Hi,
      If you use explicit implementation to implement the methods then there will be no issue. If you do a implicit implementation of the method, say the one having return type as void, then the compiler will not be happy. It will throw an error saying the following error
      "Error 1 'XYZClass' does not implement interface member 'i2.foo()'. 'XYZClass.foo()' cannot implement 'i2.foo()' because it does not have the matching return type of 'int'".

      Delete
  18. Hi,
    I have same scenario but in Java.
    I checked, Java compiler allows define 2 methods with same name and a class implementing both these methods(from 2 different Interfaces).
    But in the implementing class, it allows only one definition.
    Could not code as: IA.PrintName();
    So.. What's ur take on this..?? How can we implement 2 methods with same name in Java Class..?? N call them explicitly..??

    ReplyDelete
  19. I am not a Java expert. But whatever little bit of googling I did I came to know that Java doesn't support prefixing a method name with an interface name. Below is a link which talks about the problem.

    ReplyDelete
  20. You can use two types of techniques

    1. Implicit Implementation Technique
    2. Explicit Implementation Technique

    .Net Training In Chennai

    ReplyDelete
  21. I didnt understand how interface is instantiated here. Interface can not be instantiated.

    ReplyDelete
    Replies
    1. I don't think we are talking about Interface instantiation. Can you be more specific about it?

      Delete
  22. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article.
    Dot Net Training in chennai | Dot Net Training in velachery

    ReplyDelete
  23. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article.
    dot net training in chennai velachery | dot net training center in chennai

    ReplyDelete
  24. Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. dot net training and placement in chennai | dot net training institute in velachery

    ReplyDelete
  25. The blog gave me idea to Implement Two interface having the same method signature in the same class my sincere thanks for sharing this post
    Dot Net Training in Chennai

    ReplyDelete
  26. really nice blog has been shared by you before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blogs.
    dot net training in chennai

    ReplyDelete
  27. It is extremely an incredible work and the manner by which you are sharing the information is excellent.Thanks for helping me to comprehend essential ideas. As a learner in Dot Net programming your post help me a lot.Thanks for your educational article.
    Technology

    ReplyDelete
  28. I found your post while searching for some related information on blog search... Its a great blog, keep posting and update the information.
    J2EE Training in Chennai
    JAVA Training Chennai

    ReplyDelete
  29. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.


    rpa Training in Chennai

    rpa Training in bangalore

    rpa Training in pune

    blueprism Training in Chennai

    blueprism Training in bangalore

    blueprism Training in pune

    rpa online training

    ReplyDelete
  30. This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolite festivity to pity. I appreciated what you ok extremely here.
    Click here:
    angularjs training in rajajinagar
    Click here:
    angularjs training in marathahalli

    ReplyDelete
  31. I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.

    Data Science training in marathahalli
    Data Science training in btm
    Data Science training in rajaji nagar
    Data Science training in chennai
    Data Science training in kalyan nagar
    Data Science training in electronic city
    Data Science training in USA
    Data science training in pune




    ReplyDelete
  32. hank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me 
    java training in chennai | java training in bangalore

    java online training | java training in pune

    java training in chennai | java training in bangalore

    ReplyDelete
  33. Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging
    Devops training in velachery
    Devops training in annanagar

    ReplyDelete
  34. I would really like to read some personal experiences like the way, you've explained through the above article. I'm glad for your achievements and would probably like to see much more in the near future. Thanks for share.
    python training in tambaram
    python training in annanagar
    python training in OMR
    python training in chennai

    ReplyDelete
  35. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
    DevOps online Training|DevOps Training in USA
    Devops Training in Chennai
    Devops Training in Bangalore

    ReplyDelete
  36. Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up. 
    Blueprism training in annanagar

    Blueprism training in velachery

    Blueprism training in marathahalli

    ReplyDelete
  37. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.

    java training in chennai | java training in USA

    ReplyDelete

Please provide your valuable comments.