If the class implements two interfaces and these two interfaces contain members with the same signature, implementing this member in the class will result in both interfaces using this member as their implementation. For example:
Interface iControl
{
Void paint ();
}
Interface isurface
{
Void paint ();
}
Class sampleclass: iControl, isurface
{
// Both isurface. Paint and iControl. Paint call this method.
Public void paint ()
{
}
}
However, if two interface members execute different functions, this may result in incorrect implementation of one of the interfaces or incorrect implementation of both interfaces. You can explicitly implement interface Members-that is, create a class member that is called only through this interface and is specific to this interface. This is implemented by using the interface name and a period to name the class members. For example:
Public class sampleclass: iControl, isurface
{
Void iControl. Paint ()
{
System. Console. writeline ("iControl. Paint ");
}
Void isurface. Paint ()
{
System. Console. writeline ("isurface. Paint ");
}
}
Class member iControl. Paint can only be used through the iControl interface, and isurface. Paint can only be used through isurface. Both methods are separated and cannot be directly used in the class. For example:
Sampleclass OBJ = new sampleclass ();
// Obj. Paint (); // compiler error.
IControl c = (iControl) OBJ;
C. Paint (); // callicontrol. Paint on sampleclass.
Isurface S = (isurface) OBJ;
S. Paint (); // callisurface. Paint on sampleclass.
Explicit implementation is also used to resolve the situation where two interfaces declare different members with the same name (such as attributes and methods:
Interface ileft
{
Int P {Get ;}
}
Interface iright
{
Int P ();
}
To implement both interfaces at the same time, the class must use explicit implementation for Attribute P and/or Method P to avoid compiler errors. For example:
Class middle: ileft, iright
{
Public int P () {return 0 ;}
Int ileft. P {get {return 0 ;}}
}