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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
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.
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

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();
    }

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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.