We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

multiple inheritance/implementation

bearware
bearware asked
on
Medium Priority
214 Views
Last Modified: 2008-03-10
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.
Comment
Watch Question

Senior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
If you use the second method then you HAVE to explicitly cast to the correct interface everytime. tt.DoStuff() alone will not work.

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

Author

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

 

Author

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 TawnSenior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
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.

Author

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

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.