0

This code compiles without an error

 class program
{
    interface IA
    {
       void getName(string s);
    }

    interface IB
    {
        void getName(string s);
    }

    interface IC
    {
        void getName(string s);
    }

   public  abstract class AC
    {
        public abstract void getName(string s);
   }

    public class CA : AC , IA, IB, IC
    {
  public override void getName(string s)
        {
            Console.WriteLine(s);
        }
    }

    static void Main()
    {
        CA ca = new CA();

        Console.ReadLine();
    }

}

Which getName method is implemented? If we have a multiple interfaces with the same method name, is it enough to implement just one method that would satisfy all the interfaces? What if they do different things? Notice that I didn't specify which getName there is (unlike the solution at this explicit naming).

Thanks all.

Community
  • 1
  • 1
Zuzlx
  • 1,246
  • 14
  • 33
  • 2
    The implementation would be used to satisfy the functions in all three interfaces. – Tripp Kinetics Jul 02 '14 at 18:05
  • @TrippKinetics Thanks. If I remove `override` then the compiler complains about the `getName` in abstract method is not not implemented. So it appears to be a one-way thing. – Zuzlx Jul 02 '14 at 18:09
  • 1
    @Zuzlx It's different for members declared in an interface and these declared in abstract class. To satisfy an interface you have to implement a method, to satisfy abstract class, you have to override a method. And because you have to explicitly say *my method overrides another one* you have to use `override` modifier. – MarcinJuraszek Jul 02 '14 at 18:11

4 Answers4

2

The method which is being overridden is being called. In order to use the method from the Interface you will have to do something along the line of...

((IB).getName(s));

You will have to explicitly call these methods.

http://msdn.microsoft.com/en-us/library/ms173157.aspx

2

In your code the method getName in class CA implements all 3 interfaces. If they have a different meaning you would have to use explicit interface implementation:

public class CA : AC, IA, IB, IC
{
    public override void getName(string s)
    {
        Console.WriteLine(s);
    }

    void IC.getName(string s)
    {
        // Your code
    }

    void IB.getName(string s)
    {
        // Your code
    }

    void IA.getName(string s)
    {
        // Your code
    }
}
dotnetom
  • 24,551
  • 9
  • 51
  • 54
1

The implementation would be used to satisfy the functions in all three interfaces.

Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37
1

I guess your Class CA,Implements all 3 interfaces and abstract method which is overridden in CA.Since it satisfies all implementation needed for your class CA,It would not throw any error.If you need to call Interface,Call them explicitly.

Rangesh
  • 728
  • 2
  • 12
  • 27