Apr
8th

4 Key Differences Between Implicit and Explicit Interface Implementation

Filed under C# | Posted by Shahar Y

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

implicit_explicit_impl

Implicit vs 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 Diamond_inheritanceraises 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?

OK, take a breath and digest this data…

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!

This is it, I hope that you will find that this post helpful. Enjoy and feel welcome to leave your comments.

Liked this post? Please, press my buttons! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • description
  • description
  • bodytext
  • Live
  • YahooMyWeb
  • Google
  • Sphinn
  • del.icio.us
  • Facebook
  • Slashdot
  • StumbleUpon
  • Technorati
  • BlinkList
  • Netvouz
  • Propeller
  • TwitThis
  • Reddit
Tags: , , , , , ,


4 Responses to “4 Key Differences Between Implicit and Explicit Interface Implementation”

  1. By rams on Apr 8, 2008 | Reply

    Thanks for explaining the differences. Nice article.

  1. 3 Trackback(s)

  2. Apr 9, 2008: Reflective Perspective - Chris Alcock » The Morning Brew #69
  3. Apr 23, 2008: WPF DATATEMPLATE FOR INTERFACES? NOT SUPPORTED | Dev102.com
  4. May 2, 2008: BLOG STATS FOR APRIL 2008 | Dev102.com

Post a Comment