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.

3
  • 2
    The implementation would be used to satisfy the functions in all three interfaces. Commented Jul 2, 2014 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. Commented Jul 2, 2014 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. Commented Jul 2, 2014 at 18:11

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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
    }
}

Comments

1

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.