What is the use of using Interface?
I heard that it is used instead of multiple inheritance and data-hiding can also be done with it.
Is there any other advantage, where are the places interface is used, and how can a programmer identify that interface is needed?
What is the difference between explicit interface implementation
and implicit interface implementation
?
To tackle the implicit/explicit question, let's say that two different interfaces have the same declaration:
interface IBiographicalData
{
string LastName
{
get;
set;
}
}
interface ICustomReportData
{
string LastName
{
get;
set;
}
}
And you have a class implementing both interfaces:
class Person : IBiographicalData, ICustomReportData
{
private string lastName;
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
}
Class Person Implicitly implements both interface because you get the same output with the following code:
Person p = new p();
IBiographicalData iBio = (IBiographicalData)p;
ICustomReportData iCr = (ICustomReportData)p;
Console.WriteLine(p.LastName);
Console.WriteLine(iBio.LastName);
Console.WriteLine(iCr.LastName);
However, to explicitly implement, you can modify the Person class like so:
class Person : IBiographicalData, ICustomReportData
{
private string lastName;
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public string ICustomReportData.LastName
{
get { return "Last Name:" + lastName; }
set { lastName = value; }
}
}
Now the code:
Console.WriteLine(iCr.LastName);
Will be prefixed with "Last Name:".
http://blogs.msdn.com/b/mhop/archive/2006/12/12/implicit-and-explicit-interface-implementations.aspx
Interfaces are very useful for
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With