Inheritance from Multiple Interfaces with the Same Method Name

Inheritance from multiple interfaces with the same method name

By implementing the interface explicitly, like this:

public interface ITest {
void Test();
}
public interface ITest2 {
void Test();
}
public class Dual : ITest, ITest2
{
void ITest.Test() {
Console.WriteLine("ITest.Test");
}
void ITest2.Test() {
Console.WriteLine("ITest2.Test");
}
}

When using explicit interface implementations, the functions are not public on the class. Therefore in order to access these functions, you have to first cast the object to the interface type, or assign it to a variable declared of the interface type.

var dual = new Dual();
// Call the ITest.Test() function by first assigning to an explicitly typed variable
ITest test = dual;
test.Test();
// Call the ITest2.Test() function by using a type cast.
((ITest2)dual).Test();

Inheriting multiple Interfaces and implementing methods with similar names at the same time - Kotlin

Not quite sure that this is what you need, but you can use both super<MyInterface1>.myFun() and super<MyInterface2>.myFun() in the same myFun function

private class MyClass(override val val1: Int, override val val2: Int): MyInterface1, MyInterface2 {
override fun myFun() {
super<MyInterface1>.myFun()
super<MyInterface2>.myFun()
}
}

Multiple inheritance from the same interface in C#

This is a difference in the explicit implementation (void IFoo.DoFoo()) vs the implicit implementation (public void DoFoo()). The compiler will use the explicit implementation first. If you provide both an explicit and implicit implementation then the difference becomes clear:

https://dotnetfiddle.net/7l9gIs


using System;

public interface IFoo
{
void DoFoo();
}

public class Bar: IFoo
{
public void DoFoo(){ Console.WriteLine("BAR!"); }
}

public class Baz: Bar, IFoo
{
void IFoo.DoFoo(){ Console.WriteLine("baz explicit!"); }
public new void DoFoo(){ Console.WriteLine("baz implicit!"); }
}

public class Program
{
public static void Main()
{
Baz baz = new Baz();
baz.DoFoo();

IFoo foo = baz;
foo.DoFoo();

Bar bar = baz;
bar.DoFoo();

IFoo foobar = bar;
foobar.DoFoo();
}
}

Output

baz implicit!
baz explicit!
BAR!
baz explicit!

Inheriting the same method from multiple interfaces

You can't overload the method like that, no - they have the same signatures. Your options are:

  • Use one implementation, which will be called by both interfaces
  • Use explicit interface implementation for one or both of the methods, e.g.

    int calc2.addsub(int x, int y)
    {
    return result2 = a - b;
    }

    If you do that for both methods, neither method will be callable on a reference of type Calculation; instead, you'd have to cast to one interface or the other before calling the method, including within the class. For example:

    public void ShowBoth()
    {
    Console.WriteLine(((calc1)this).addsub(5, 3)); // 8
    Console.WriteLine(((calc2)this).addsub(5, 3)); // 2
    }

Inherit interfaces which share a method name

This problem doesn't come up very often. The solution I'm familiar with was designed by Doug McIlroy and appears in Bjarne Stroustrup's books (presented in both Design & Evolution of C++ section 12.8 and The C++ Programming Language section 25.6). According to the discussion in Design & Evolution, there was a proposal to handle this specific case elegantly, but it was rejected because "such name clashes were unlikely to become common enough to warrant a separate language feature," and "not likely to become everyday work for novices."

Not only do you need to call Name() through pointers to base classes, you need a way to say which Name() you want when operating on the derived class. The solution adds some indirection:

class Interface1{
public:
virtual void Name() = 0;
};

class Interface2{
public:
virtual void Name() = 0;
};

class Interface1_helper : public Interface1{
public:
virtual void I1_Name() = 0;
void Name() override
{
I1_Name();
}
};

class Interface2_helper : public Interface2{
public:
virtual void I2_Name() = 0;
void Name() override
{
I2_Name();
}
};

class RealClass: public Interface1_helper, public Interface2_helper{
public:
void I1_Name() override
{
printf("Interface1 OK?\n");
}
void I2_Name() override
{
printf("Interface2 OK?\n");
}
};

int main()
{
RealClass rc;
Interface1* i1 = &rc;
Interface2* i2 = &rc;
i1->Name();
i2->Name();
rc.I1_Name();
rc.I2_Name();
}

Not pretty, but the decision was it's not needed often.



Related Topics



Leave a reply



Submit