-1

How can we implement two or more interfaces with same method name into the class and the derived class should be further inherit into new class with same methods.

using System;  

interface A  
{  
    void Hello();  
}  

interface B  
{  
    void Hello();  
}  

class Test : A, B  
{  
    void A.Hello()  
    {  
        Console.WriteLine("Test Hello-A");  
    } 

    void B.Hello()  
    {  
        Console.WriteLine("Test Hello-B");  
    }  
}  

class Demo : Test {
   //what will be the code to override Hello method
}

public class MainClass  
{  
    public static void Main()  
    {  
        //How can we access the Hello method of Test & Demo class  
    }  
}
Dinesh Gaud
  • 121
  • 1
  • 10
  • 4
    Small correction: You don't inherit interfaces; you implement them. – 41686d6564 stands w. Palestine Jul 24 '21 at 15:44
  • 1
    What is the question? What code you want to override? – Serge Jul 24 '21 at 15:45
  • Does this answer your question? [Inheritance from multiple interfaces with the same method name](https://stackoverflow.com/questions/2371178/inheritance-from-multiple-interfaces-with-the-same-method-name) –  Jul 24 '21 at 15:50
  • @OlivierRogier This is not answering the question as there is here a more complex case because of the `Demo` class and its needs. But this is definitely something that helps. – Misha Zaslavsky Jul 24 '21 at 16:02

3 Answers3

4

There are some limitations when working with explicit interface implementations because the methods can't be virtual. But you can do a workaround to resolve this issue by creating more methods that can be virtual and then override them.

I am not too happy with this code but it fully answers your question.

using System;

interface A
{
    void Hello();
}

interface B
{
    void Hello();
}

class Test : A, B
{
    public virtual void HelloA()
    {
        Console.WriteLine("Test Hello-A");
    }

    public virtual void HelloB()
    {
        Console.WriteLine("Test Hello-A");
    }

    void A.Hello()
    {
        this.HelloA();
    }

    void B.Hello()
    {
        this.HelloB();
    }
}

class Demo : Test
{
    //what will be the code to override Hello method
    public override void HelloA()
    {
    }

    public override void HelloB()
    {
    }
}

public class MainClass
{
    public static void Main()
    {
        //How can we access the Hello method of Test & Demo class
        Test test = new();
        test.HelloA();
        test.HelloB();

        Demo demo = new();
        demo.HelloA();
        demo.HelloB();

        // Another option will be to call it using the interfaces:
        List<A> itemsA = new();
        itemsA.Add(test);
        itemsA.Add(demo);

        foreach (A itemA in itemsA)
        {
            itemA.Hello();
        }

        List<B> itemsB = new();
        itemsB.Add(test);
        itemsB.Add(demo);

        foreach (B itemB in itemsB)
        {
            itemB.Hello();
        }
    }
}
Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
2

If you have a explicit private implementation as in the code shown, you would need to explicitly implement A and B in Demo again.

class Demo : Test , A, B
{
    void A.Hello()
    {
        Console.WriteLine("Demo Hello-A");
    }

    void B.Hello()
    {
        Console.WriteLine("Demo Hello-B");
    }
}

However you will, as far as I can see, not be able to invoke the implementations in Test from an instance of Demo, no matter how you'd cast it:

    var demo   = new Demo();
    var a_demo = (A)demo;
    var b_demo = (B)demo;
    var test   = (Test)demo;
    var a_test = (A)test;
    var b_test = (B)test;

    a_demo.Hello();
    b_demo.Hello();
    a_test.Hello(); //invokes impl in demo
    b_test.Hello(); //invokes impl in demo

    
Kit
  • 20,354
  • 4
  • 60
  • 103
lidqy
  • 1,891
  • 1
  • 9
  • 11
1
class Program
{
    static void Main()
    {
        IA objIa = new AB();
        objIa.Hello(10,20);
        IB objIb = new AB();
        objIb.Hello(10,20);
        ABC abc = new ABC();
        abc.Hello1(10,20);
        IA obj = new ABC();
        obj.Hello(10,20);
        IB obj1 = new ABC();
        obj1.Hello(10,20);
        Console.ReadLine();
    }
}

interface IA
{
    void Hello(int a, int b);
}
interface IB
{
    void Hello(int a, int b);
}

public class AB : IA, IB
{
    void IA.Hello(int a, int b)
    {
        Console.WriteLine("IA method");
    }

    void IB.Hello(int a, int b)
    {
        Console.WriteLine("IB method");
    }

    public virtual void Hello1(int a, int b)
    {
        Console.WriteLine("Hello1  method");
    }
}

public class ABC : AB,IA,IB
{
    void IA.Hello(int a, int b)
    {
        Console.WriteLine("ABC method");
    }

    void IB.Hello(int a, int b)
    {
        Console.WriteLine("ABC method");
    }
    public override void Hello1(int a, int b)
    {
        //base.add1(a, b);
        Console.WriteLine("Hello1 override method");
    }
}
Jonathan
  • 1,955
  • 5
  • 30
  • 50
kundan
  • 44
  • 3
  • please explain your solution! – Jonathan Jul 24 '21 at 20:57
  • 1
    If both interface(IA & IB) having same method then we will go for explicit implementation under Class AB. If we want to inherit class AB for class ABC then it will inherit only other methods (Hello1) i.e. not available under both interfaces. Suppose we want to use interface method then we have to inherit that interface in class ABC. Implicit or Explicit interface implemented methods can't override in other child class. Could you please let me know, is it clear explanation or not? I will be happy to explain you. – kundan Jul 25 '21 at 13:30
  • I am moderating although I'm not into that topic too much. Considering your answer as well as the question will stay here for years, it's helpful for others in the future to get a deeper understanding of the matter. Thanks for your explanation. – Jonathan Jul 25 '21 at 13:59