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.

30 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

Please provide your valuable comments.