简体   繁体   中英

My program has two interfaces and both have a method with the same name. So, how can we implement them in a child class inheriting both interfaces?

“My program has two interfaces and both have a method with the same name. So, how can we implement them in a child class which is inheriting both the interfaces? Also how can we call both the methods from the child class?”

public interface A
{
    void Display();
}
public interface B
{
    void Display();
}

class Program: A, B
{
    void A.Display()
    {
        Console.WriteLine("Method of interface A");
    }
    void B.Display()
    {
        Console.WriteLine("Method of interface B");
    }
}

Is it the right way to implement both the interface methods?

Yes you are doing it in the correct way.

When working with interfaces, there occurs a situation when a class implements two interfaces and both the interfaces contain a member with the same signature. When the class provides a definition to interface members, it gets confused about which member gets the definition since both have the same name. In that case, we'll use an Explicit Interface Implementation.

See the classic example below.

using System;    

namespace InterfaceDemo    
{    
     //First Interface IDebitCard    
     interface IDebitCard    
     {     
       void CardNumber();    
     }    

     //Second Interface ICreditCard    
     interface ICreditCard    
     {    
       void CardNumber();    
     }    

     //Customer Class implements both the interfaces    
     class Customer: IDebitCard, ICreditCard    
     {   

       void IDebitCard.CardNumber()    
       {    
         Console.WriteLine("Debit Card Number: My Card Number is 12345XXXXX");    
       }    

       void ICreditCard.CardNumber()    
       {    
          Console.WriteLine("Credit Card Number: My Card Number is 98999XXXXX");    
       }    

       public void CardNumber()    
       {    
          Console.WriteLine("Customer ID Number: My ID Number is 54545XXXXX");    
       }    
     }    

     class Program    
     {    
       static void Main(string[] args)    
       {    
          Console.WriteLine("////////////////////- Implicit and Expliction Implementation -//////////////////// \n\n");    
          Customer customer = new Customer();    
          IDebitCard DebitCard = new Customer();    
          ICreditCard CreditCard = new Customer();    

          customer.CardNumber();    
          DebitCard.CardNumber();    
          CreditCard.CardNumber();    

          Console.ReadKey();    
       }    
     }    
} 

you can check the full article here : Link

Assuming you want 2 different logic's for the 2 different interface implementations, then you will have to explicitly cast the explicit interface implementations

 var program = new Program();
 ((A)program).Display();
 ((B)program).Display();

Output

Method of interface A
Method of interface B

It makes sense really, the compiler (or anyone for that matter) can possibly know which want you want, the caller has to choose via a cast

Explicit Interface Implementation (C# Programming Guide)

If a class implements two interfaces that contain a member with the same signature, then implementing that member on the class will cause both interfaces to use that member as their implementation.

If the two interface members do not perform the same function, however, this can lead to an incorrect implementation of one or both of the interfaces. It is possible to implement an interface member explicitly—creating a class member that is only called through the interface, and is specific to that interface . This is accomplished by naming the class member with the name of the interface and a period.

What are you trying to achieve?

You could do something like this:

public interface A
{
  void Display();
}

public interface B : A
{
  void SomeOtherMethod();
}

class Program: B
{
  public void Display()
  {
    // implementation
  }

  public void SomeOtherMethod()
  {
    // implementation
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM