?
Solved

multiple inheritance/implementation

Posted on 2006-03-31
7
Medium Priority
?
201 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.
0
Comment
Question by:bearware
  • 3
  • 3
7 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 16344085
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16344101
If you use the second method then you HAVE to explicitly cast to the correct interface everytime. tt.DoStuff() alone will not work.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16344111
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:bearware
ID: 16358353
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
 
LVL 1

Author Comment

by:bearware
ID: 16358432
<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
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 16358521
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
 
LVL 1

Author Comment

by:bearware
ID: 16361115
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question