multiple inheritance/implementation

If I inherit 2 or more interfaces that have the same method name/signiture what happens.

Do I get to implement it one, twice or will it not build; or something else.
LVL 1
bearwareAsked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
You can implement one method that satisfies the signature for both interfaces.

    class ThisThat : IDoThis, IDoThat
    {
        public void DoStuff()
        {
            Console.WriteLine("Doing stuff");
        }
    }

    interface IDoThis
    {
        void DoStuff();
    }

    interface IDoThat
    {
        void DoStuff();
    }

    static void Main()
    {
        ThisThat tt = new ThisThat();
        tt.DoStuff();
    }

Or, you can specify one for each, in which case you have to cast to the interface you want to use:

    class ThisThat : IDoThis, IDoThat
    {
        void IDoThis.DoStuff()
        {
            Console.WriteLine("IDoThis doing stuff");
        }

        void IDoThat.DoStuff()
        {
            Console.WriteLine("IDoThat doing stuff");
        }
    }

    interface IDoThis
    {
        void DoStuff();
    }

    interface IDoThat
    {
        void DoStuff();
    }

    static void Main(string[] args)
    {
        ThisThat tt = new ThisThat();
        ((IDoThis)tt).DoStuff();
        ((IDoThat)tt).DoStuff();

        Console.ReadLine();
    }

Hope this helps.
0
 
Carl TawnSystems and Integration DeveloperCommented:
If you use the second method then you HAVE to explicitly cast to the correct interface everytime. tt.DoStuff() alone will not work.
0
 
AlexFMCommented:
You need to implement both methods providing full qualified name:

interface IDimensions
{
    float getLength();
    float getWidth();
}

interface ISize
{
    float getLength();
    float getWidth();
}

class Box : IDimensions, ISize
{
    float IDimensions.getLength()
    {
        ...
    }

    float IDimensions.getWidth()
    {
        ...
    }

    float ISize.getLength()
    {
        ...
    }

    float ISize.getWidth()
    {
        ...
    }
}

From the client code, you can call these functions using interface reference.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
bearwareAuthor Commented:
That explains most of it.

I assume using the first example, I could rewrite main as:

static void Main(string[] args)
    {
        ThisThat tt = new ThisThat();


        ((IDoThis)tt).DoStuff();
        ((IDoThat)tt).DoStuff();
    }

 
0
 
bearwareAuthor Commented:
<correction to previus post, sorry about that>

That explains most of it.

I assume using the first example, I could rewrite main as:

    static void Main(string[] args)
    {
        ThisThat tt = new ThisThat();
        IDoThis DoThis = tt;
        IDoThat DoThat =tt

        DoThis.DoStuff();
        DoThat.DoStuff();
    }

The extra bit im not so shour about is this example:

interface IDo
{
        void DoStuff();
}

 interface IDoThis : IDo
 {
 }

 interface IDoThat : IDo
 {
 }
 
class ThisThat : IDoThis, IDoThat
{
    void IDoThis.DoStuff()
    {
        Console.WriteLine("IDoThis doing stuff");
    }

    void IDoThat.DoStuff()
    {
        Console.WriteLine("IDoThat doing stuff");
    }
}

 static void Main(string[] args)
{
        ThisThat tt = new ThisThat();
        IDo Do = tt;

        Do.DoStuff();          //which one is called
        ((IDo)tt).DoStuff(); //which one is called
}

Thanks in advance
Richard.
0
 
Carl TawnSystems and Integration DeveloperCommented:
That code shouldn't compile.

DoStuff doesn't explicitly belong to the IDoThis and IDoThat interfaces, it belongs to the IDo interface. So in order to explicitly implement it you would have to use IDo.DoStuff().

Because DoStuff is the base for both IDoThis and IDoThat then what you end up with is an merger or the unique signatures, so there is only one DoStuff() method rather than on for each of the two derived interfaces. Therefore, no explicit declaration is needed, you simply use: void DoStuff(), to implement the method as defined in the base interface.

In practical terms you shouldn't realisticly inherit a class from two interfaces with the same base.

Hope that made sense.
0
 
bearwareAuthor Commented:
yep Thanks.

Looks like it is well defined.
C# is a lot more difficalt than eiffel, and not so powerful. but I think I am starting to understand it.

Thanks

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.