Beginning OOP: How to implement and call Interfaces with a conflicting method name in same Class

AID: 7330
  • Status: Published

1550 points

  • Bygopaltayde
  • TypeTips/Tricks
  • Posted on2011-09-03 at 07:04:09
This article is for Object-Oriented Programming (OOP) beginners.

An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Interface member. In C# multiple inheritance is achieved with the help of Interfaces.

Therefore, if the Development Team is large or 3rd-party vendors are involved, then it may possible that two team members may end up creating two different Interfaces with the same method name. Consequently, as a developer you may have to implement both these Interfaces in one Class.

Scenario: So let’s assume that –

You have two different Interfaces let’s say ICustomer and IVendor. Both the Interfaces have a method with same name let's say GetData().

GetData() from ICustomer would return the list of customer names while GetData() from IVendor would return the list of vendor names. In the data access layer, a single class CompanyDAL implements both these Interfaces.

See attached Code Snippet for Interface definitions –
using System.Collections.Generic;

namespace ExplicitInterfacesSite
{
    // Simple interface for Vendor class
    public interface IVendor
    {
        // Return the list of vendor names
        List<string> GetData();
    }

    // Simple interface for customer class
    public interface ICustomer
    {
        // Return the list of  customer names.
        // This function name is conflicting with IVendor's GetData.
        List<string> GetData();

        // Another method, which is not conflicti
        string GetCustomerAddress(int customerId);
    }
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:

Select allOpen in new window



Solution:

While implementing the Interface in a class, we must provide the concrete implementation to each of the Interface member. For non-conflicting Interface members, we can just write the member name and provide the implementation. But for any method which is common in both the Interfaces, the member should be implemented explicitly in the class.
By explicit, I mean the method name should be prefixed with <InterfaceName.>. In our case it is like IVendor.GetData () or ICustomer.GetData().

See Code Snippet –
using System.Collections.Generic;

namespace ExplicitInterfacesSite
{
    public class CompanyDAL : IVendor, ICustomer
    {
        #region IVendor Members

        List<string> IVendor.GetData()
        {
            // Implement your logic to get the list of Vendor  Names
            return new List<string>() {"Vendor1", "Vendor2", "Vendor3"};
        }

        #endregion

        #region ICustomer Members

        // This is how we should implement the conflicting method, explicitely 
        List<string> ICustomer.GetData()
        {
            // Implement your logic to get the list of Customer Names
            return new List<string>() {"Customer1", "Customer2"};
        }

        // No need of Interface.MethodName as there is no conflict.
        public string GetCustomerAddress(int customerId)
        {
            return "Dummy Customer Address";
        }

        #endregion
    }
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:

Select allOpen in new window



And when we want to call that method, we need to typecast the class object to the desired Interface.

I have added an .aspx page in the project to demonstrate this. It just prints the list of vendors and customers.

See the attached Code Snippet –
 
using System;
using System.Collections.Generic;

namespace ExplicitInterfacesSite
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Create the object
            CompanyDAL expliciteObj = new CompanyDAL();

            // Can directly call method, whose name is not confliciting
            expliciteObj.GetCustomerAddress(1);

            // Cast the object to the interface first and then call  the conflicting method.
            // Here we are calling the GetData() method of IVendor class.
            List<string> lstVendors = (expliciteObj as IVendor).GetData();
            Response.Write("Vendor List:</br>");
            foreach (string vendorName in lstVendors)
            {
                Response.Write(vendorName);
                Response.Write("</br>");
            }

            // Cast the object to the interface first and then call  the conflicting method.
            // Here we are calling the GetData() method of ICustomer class.
            List<string> lstCustomers = (expliciteObj as ICustomer).GetData();
            Response.Write("Customer List:</br> ");
            foreach (string customerName in lstCustomers)
            {
                Response.Write(customerName);
                Response.Write("</br>");
            }
        }
    }
}
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:

Select allOpen in new window



Let's look at the below code line carefully –
List<string> lstVendors = (expliciteObj as IVendor).GetData();
                                    
1:

Select allOpen in new window


I first typecast the expliciteObj to the Interface type, and then called the conflicting method. By doing this we are able to remove the conflict/ambiguity in the method name. This would return a list of string. It should contain below strings (vendor names):
"Vendor1", "Vendor2" and "Vendor3".

The same way in the below code line –
List<string> lstCustomers = (expliciteObj as ICustomer).GetData();
                                    
1:

Select allOpen in new window


expliciteObj is first converted to ICustomer and then called the method GetData. This call would return a list of customer names. In our case it should be:
"Customer1" and "Customer2".

Summary: In the above article I tried to explain how to achieve multiple Interface inheritance in C# and how to handle conflicting methods present in more than one Interface. I hope you will enjoy reading this article. Waiting for your feedback.
Asked On
2011-09-03 at 07:04:09ID7330
Tags

OOPs

,

explicitly Implementing Interfaces

,

Casting to interface

Topic

C# Programming Language

Views
973

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top C# Experts

  1. kaufmed

    566,376

    Sage

    500 points yesterday

    Profile
    Rank: Genius
  2. BuggyCoder

    240,764

    Guru

    10 points yesterday

    Profile
    Rank: Sage
  3. navneethegde

    158,560

    Guru

    0 points yesterday

    Profile
    Rank: Wizard
  4. CodeCruiser

    140,708

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  5. TheLearnedOne

    137,350

    Master

    2,800 points yesterday

    Profile
    Rank: Savant
  6. wdosanjos

    124,511

    Master

    3,500 points yesterday

    Profile
    Rank: Genius
  7. AndyAinscow

    107,357

    Master

    0 points yesterday

    Profile
    Rank: Genius
  8. Dhaest

    98,335

    Master

    0 points yesterday

    Profile
    Rank: Genius
  9. Idle_Mind

    91,914

    Master

    0 points yesterday

    Profile
    Rank: Savant
  10. tommyBoy

    90,068

    Master

    0 points yesterday

    Profile
    Rank: Genius
  11. nishantcomp2512

    89,000

    Master

    0 points yesterday

    Profile
    Rank: Wizard
  12. MlandaT

    86,553

    Master

    0 points yesterday

    Profile
    Rank: Genius
  13. Chinmay_Patel

    80,818

    Master

    0 points yesterday

    Profile
    Rank: Genius
  14. ddayx10

    66,538

    Master

    0 points yesterday

    Profile
    Rank: Sage
  15. anarki_jimbel

    66,132

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  16. ambience

    63,764

    Master

    0 points yesterday

    Profile
    Rank: Sage
  17. ukerandi

    62,593

    Master

    1,000 points yesterday

    Profile
    Rank: Guru
  18. apeter

    60,772

    Master

    0 points yesterday

    Profile
    Rank: Sage
  19. JamesBurger

    60,305

    Master

    0 points yesterday

    Profile
    Rank: Sage
  20. sedgwick

    52,750

    Master

    1,600 points yesterday

    Profile
    Rank: Genius
  21. emoreau

    50,917

    Master

    0 points yesterday

    Profile
    Rank: Genius
  22. ged325

    50,311

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  23. anuradhay

    49,977

    2,500 points yesterday

    Profile
    Rank: Guru
  24. techChallenger1

    47,638

    0 points yesterday

    Profile
    Rank: Guru
  25. mas_oz2003

    43,540

    0 points yesterday

    Profile
    Rank: Genius

Hall Of Fame