Zuzlx
Zuzlx

Reputation: 1266

Polymorphism, abstract and interface

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.

Upvotes: 0

Views: 689

Answers (4)

Rangesh
Rangesh

Reputation: 728

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.

Upvotes: 1

Tripp Kinetics
Tripp Kinetics

Reputation: 5439

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

Upvotes: 1

dotnetom
dotnetom

Reputation: 24901

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

Upvotes: 2

Muhammad Zunair
Muhammad Zunair

Reputation: 19

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

Upvotes: 2

Related Questions