Inerface was introduced to revoke the drawbacks of multiple inheritance. Moreover providing support for explicit interface implementation is the most fascinating thing for a developer who thinks in OO way. Now you can implement multiple interfaces in a class and still there is no ambiguity gets introduced.Apart from that , Explicit interface implementation actually create the whole implementation of the interface private , that is , to access the member of a class that implement the interface explicitly , interface reference is needed which supports the so-called concepts named “Run-time Bindings” and introduces a higher level of Abstraction .
In this Code sample, I am going to start from the simple interface definition and take a step at a time to reach up to Explicit interface implementation so that everyone could be on the same page. By this time, you probably understand that in the rest of the Code Sample I am going to focus on following Agenda –
1. Defining Interface.
2. Explicit Implementation of the Interface .
3. Advantage of Explicit Interface ImplementationCode Sample :
public interface ISimpleCalculator{
int Add(int num1, int num2);
}
public interface ICalculator
{
double Add(double num1, double num2);
}
///
/// Benefits of Explicit Interface -
/// 1. Private Implementation
/// 2. Remove Ambiguity while Implementing Mutiple Interface
///
public class Calculator:ISimpleCalculator , ICalculator
{
#region ISimpleCalculator Members
int ISimpleCalculator.Add(int num1, int num2)
{
return num1 + num2;
}
#endregion
#region ICalculator Members
double ICalculator.Add(double num1, double num2)
{
return num1 + num2;
}
#endregion
}
classProgram
{
static void Main(string[] args)
{
// Example - 1 : Private Implementation
Calculator calc = new Calculator();
/// Uncomment the following line and try to run the program, You will get a compile time error. Becauause , Members
/// of the Calculator class is Explicitly implements two interfaces. To Access the members of the class , there is only
/// one way … that is - Access it through INTERFACE REFERENCE.
//calc.Add(); // Uncomment it to get the Compile time Error
ISimpleCalculator simpleCalc = calc;
int result = simpleCalc.Add(5, 10);
Console.WriteLine(”Using ISimpleCalculator : “);
Console.WriteLine(”5+10 = ” + result);
Console.WriteLine(”————————————–”);
// Example -2 : Explicit Implementation to remove Ambiguity
/// If you have a look at the ICalculator and ISimpleCalculator interfaces - both these interfaces has one common
/// funationality - Add and our Calcultor class implement both the Interface ,that is , Calculator class has to
/// implement the Add funtionality twice . So .. The Question is - whether it gonna create Ambiguty? That’s the beuty
/// of Explicit Implementaion of Interface that it not gonna Create any Ambiguity
That’s the beauty of POLYMOPHIC
/// REFERENCE
double num1 = 3.2;
double num2 = 5.8;
ICalculator floatingNumberCalculator = calc;
Console.WriteLine(”Using ICalculator : “);
Console.WriteLine(num1 +”+”+num2+” = “+ floatingNumberCalculator.Add(num2 ,num1)) ;
Console.WriteLine(”————————————–”);
}
}
So the key Take-away from Explicit Interface Implementation is that -1. It make the whole implementation Abstract/Private . That is , you need the interface reference to access that implementation.
2. It allow you to implement multiple Interface and at the same time , does not introduce any ambiguity. So , the polymorphic reference or runtime binding can be implement very efficiently.
“Code 4 Fun and Keep it Stylish”
~Adil