Implicit vs explicit interface implementation


Whenever we need to implement an interface in C#, two options pops up (click Ctrl+’.’).

  • Implicit interface implementation 
  • Explicit interface implementation

What shall be selected? before making a decision, lets understand what is the difference between those two.

Solving the diamond problem
The diamond problem is related to object oriented languages that allow multiple inheritance. The problem raises when there is base class A, classes B and C derived from A and class D derived from B and C. B and C, both have a method “Foo”, that has been overridden differently. If this method is called in D, then via which class is it inherited: B or C?

As we know C# does not support multiple inheritance of classes, but it does support multiple inheritance of interfaces. Therefore, the same ambiguity may arise unless explicit interface implementation is used. Going explicitly, we can have different implementations to the same method. The problem is solved…

interface IRegularAccess
{
    void ApproveLoan();
}        

interface IAdministratorAccess
{
    void ApproveLoan();
}        

class BankDatabaseProxy : IRegularAccess, IAdministratorAccess
{
    void IRegularAccess.ApproveLoan()
    {
        // Code for regular access approve loan.
    }         

    void IAdministratorAccess.ApproveLoan()
    {
        // Code for administrator access approve loan.
    }
}

A good example of such a usage is a bank, where one class implements 2 interfaces to the data-base proxy: IRegularAccess and IAdministratorAccess.It is clear that the ApproveLoan method shall not be implemented in the same manner for both cases.
Private and Public issues:

In implicit implementation, the public keyword is attached to the method (otherwise, compilation error). It is the opposite with explicit implementation (where the private keyword is used). This is how the implicit implementation looks with our example:

class BankDatabaseProxy : IRegularAccess, IAdministratorAccess
 {
     public void ApproveLoan()
     {
         // Insert your code.
     }
 }

The result is that the explicit implementation keeps the interface members out of the class (and out of the Intellisense too). Again, it is much easier to demonstrate this issue in code:

// Does not compile because of explicit implementation.
BankDatabaseProxy bankDB = new BankDatabaseProxy();
bankDB.ApproveLoan();        

// This is good.
IAdministratorAccess adminAccessToBankDB = new BankDatabaseProxy();
adminAccessToBankDB.ApproveLoan();

No Virtual for Explicit:
Explicit method implementation can not be virtual, whereas an implicit method can. Child classes won’t be able to override an explicit method.

No Abstract for Explicit:
Explicit method implementation can not be abstract, whereas an implicit method can. Class that use the explicit implementation can not be abstract!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s