Inherit Interfaces Which Share a Method Name

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.

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

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

Is it possible to define a shared method for C# interfaces?

The "almost always" is the problem. If it was "always", then an "extension method" would be ideal:

public static class FubarExtensions
{
public static void SomeMethod(this IFubar obj) { ... }
}

If you need the "almost", they you probably need polymorphism for it to be reliable. In that case, using a static method for the default implementation is probably your best option, i.e.

IFubar.SomeMethod(...)
{
Fubar.SomeMethodDefault(this, ...);
}

or:

public virtual void SomeMethod(...)
{
Fubar.SomeMethodDefault(this, ...);
}

(or any similar combination)

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!

if two interface having same method name that time how we achieve multiple inheritance in C#

You may implement the interfaces explicit.

class C : A, B
{
void A.Method()
{
// explicit implementation of interface A
}

void B.Method()
{
// explicit implementation of interface B
}

public void Method()
{
// class implementation
}
}

You should be aware of how to invoke the particular methods.

var c = new C();

c.Method(); // class implementation
((B)c).Method(); // implementation of B
((A)c).Method(); // implementation of A

Naming conflict: Same method name in inherited class and interface

The inherited class implements your interface method, so there should not be an error. In fact, both having the same name is really the idea of implementing an interface...

Here's a check list:

  • The method must have not only the same name, but the same signature. Make sure you've specified the correct argument and return types (this includes initial values).
  • If your sub class A also implements the same method, you must mark it as override. Same rules apply regarding the signature.
  • If you do override B's method, it must not be declared final.


Related Topics



Leave a reply



Submit