Introduction
This article gives an idea about the situation called interface clash. This situation occurs when two interfaces have functions with the same name and signature and one base class implements these interfaces.
Background
In earlier languages like C++, there was the concept of Multiple Inheritance. But in C#, we do not have any such feature. This was something in the list of features which is available in C++ but not in C#. Due to this feature in C++, developers were facing the problem of Diamond Inheritance. That’s why Virtual Functions came into the picture.
Using the Code
In C#, when two interfaces have functions with the same name and a class implements these interfaces, then we have to specifically handle this situation. We have to tell the compiler which class function we want to implement. For such cases, we have to use the name of the interface during function implementation. Have a look at the following example:
Blocks of code should be set as style Formatted
like this:
Ex:
public interface Interface1
{
void MyInterfaceFunction();
}
public interface Interface2
{
void MyInterfaceFunction();
}
public class MyTestBaseClass:Interface1,Interface2
{
#region Interface1 Members
void Interface1.MyInterfaceFunction()
{
MessageBox.Show("Frm MyInterface1 Function()");
return;
}
#endregion
#region Interface2 Members
void Interface2.MyInterfaceFunction()
{
MessageBox.Show("Frm MyInterface2 Function()");
return;
}
#endregion
}
In the above example, we are implementing the function MyInterfaceFunction()
by using its interface name. In this case if we create the object of MyTestBaseClass
and check for MyInterfaceFunction()
, it won't be directly available. Look at the following code:
MyTestBaseClass obj = new MyTestBaseClass();
obj.MyInterfaceFunction();
We can call the respective interface function by typecasting it to the corresponding interfaces. See the example below:
((Interface1)obj).MyInterfaceFunction();
((Interface2)obj).MyInterfaceFunction();
Now comes the twist. We will add one more function with the same name in MyTestBaseClass
. Now try to access the elements of the object of MyTestBaseClass
. You will see that MyInterfaceFunction
is available there. See the following code now and this code would call the function of interfaces first followed by the Class
function.
Ex.
public class MyTestBaseClass:Interface1,Interface2
{
public void MyInterfaceFunction()
{
MessageBox.Show("Frm MyTestBaseClass's Function()");
return;
}
#region Interface1 Members
void Interface1.MyInterfaceFunction()
{
MessageBox.Show("Frm MyInterface1 Function()");
return;
}
#endregion
#region Interface2 Members
void Interface2.MyInterfaceFunction()
{
MessageBox.Show("Frm MyInterface2 Function()");
return;
}
#endregion
}
public interface Interface1
{
void MyInterfaceFunction();
}
public interface Interface2
{
void MyInterfaceFunction();
}
In the Main Function –
MyTestBaseClass obj = new MyTestBaseClass();
((Interface1)obj).MyInterfaceFunction();
((Interface2)obj).MyInterfaceFunction();
obj.MyInterfaceFunction();
Now comes another twist. You derive a class MyDerivedClass()
from MyTestBaseClass()
. Before that, remove the function last implemented in MyTestBaseClass()
. Now create the instance of MyDerivedClass()
and check. No function would be available with this class even though its base class is having these functions.
Ex:
public interface Interface1
{
void MyInterfaceFunction();
}
public interface Interface2
{
void MyInterfaceFunction();
}
public class MyTestBaseClass:Interface1,Interface2
{
#region Interface1 Members
void Interface1.MyInterfaceFunction()
{
MessageBox.Show("Frm MyInterface1 Function()");
return;
}
#endregion
#region Interface2 Members
void Interface2.MyInterfaceFunction()
{
MessageBox.Show("Frm MyInterface2 Function()");
return;
}
#endregion
}
public class MyDerivedClass : MyTestBaseClass
{
}
So again here for getting the function of interfaces, we have to separately typecast the object of the derived class with the interface. See the code below:
MyDerivedClass derivedObj = new MyDerivedClass();
((Interface1)derivedObj).MyInterfaceFunction();
Conclusion
From the above article, you should get an idea about calling interface functions when there is a clash. Your suggestions and views will be appreciated.