Eric Bergman-Terrell's Blog

.NET Programming Tip: How can a Class Implement Multiple Interfaces Containing Methods with Identical Signatures?
October 4, 2010

For example, you have two interfaces (I1 & I2) that both contain a Send method with the same signature (identical method name, same return type, same argument types in the same order:

public interface I1
{
int Send(int id);
}

public interface I2
{
int Send(int id);
}

If you don't care which interface was used to make the call to the Send method, just implement the method:

public class c1 : I1, I2
{
int Send(int id)
{
return 0;
}
}

If you do care about which interface was used to make the Send call, use Explicit Interface Implementation by specifying the interface name before the method name (I1.Send, I2,Send):

public class c1 : I1, I2
{
int I1.Send(int id)
{
return 1;
}

int I2.Send(int id)
{
return 2;
}
}

In this case (when using Explicit Interface Implementation) the methods cannot be declared as public, although they are public (they can be called from code outside of the class). In other words, they're public with respect to the interface, but not with respect to the class.

Keywords: Interfaces, Explicit Interface Implementation

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
How to decompile Java code with JetBrains IntelliJ IDEA (2018.2.3, Windows 10)October 5, 2018
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015