[ACCEPTED]-How do interfaces solve the diamond problem?-diamond-problem

Accepted answer
Score: 13

Interfaces guarantee that a given set of methods 49 will be implemented. The interface does 48 not concern itself on how will the code 47 be implemented. As opposed to extending 46 classes, when you extend a class you are 45 using some predefined code.

If you extend 44 two classes with the same method, say, methodA, and 43 call this method, then, the compiler will 42 not know which one will it need to run. On 41 the other hand, when you implement two interfaces 40 with the same method, since the implementation 39 is written within your own code, the compiler 38 will know where it needs to go to find the 37 implementation.

So basically, when you implement 36 something you are guaranteeing you will 35 be offering some service.

EDIT: I'll try 34 and take your example and turn it in some 33 Object Oriented way, this will hopefully, make 32 things clearer.

Jack and Ben will guarantee 31 that they will lend you the money, you, on 30 the other hand, guarantee that will pay 29 it back, so, from an OO point of view you 28 can see it like this:

public interface IPay
{
    public void Pay(Individual i);
}

public class MySelf : IPay
{
    public void Pay(Individual i)
    {
        ...
    }
}

public interface ILend
{
    public void Lend(Individual i);
}

public class Jack : ILend //Same applies for Ben
{
    public void Lend(Individual i)
    {
        ...
    }
}

This is how I would 27 go about the problem you have specified.

As 26 for the other trash example, this is how 25 you can go about it:

public interface ITakeTrash
{
    public void TakeTrash(Trash t);
}

public Son : ITakeTrash
{
    public void TakeTrash(Trash t);
}

public class Mum  //Same applies for dad
{
    Son mySon = ...

    mySon.TakeTrash(t);
}

Both your mother and 24 father do not know what to do with the trash, but, they 23 know that you know how to deal with it, and 22 that is what they care about (just like 21 above, you only care that Ben and Jack can 20 give you money and the other parties just 19 care that you can pay it back, none of the 18 involved really care how you do it.

I do understand 17 where you are trying to go, basically saying 16 what if I implement two different interfaces 15 with methods having the same name that do 14 different things. If you ever end up in 13 such a scenario, I think that that would 12 be due to bad design. I would not recommend 11 building a single class which does a myriad 10 of different things, this being one of the 9 reasons.

On the other hand, you might ask, what 8 if I have 2 interfaces that do similar things 7 with methods having the same name? Then, I 6 think that the answer would be that since 5 the interfaces do similar things and you have 4 methods with the same name then, it is most 3 likely that you are doing the same thing, hence, you 2 need not confuse yourself.

I hope this edit 1 helped shed some light on the issue.

Score: 5

Well implementing methods is not like giving 4 money. It's like taking out the trash. If 3 you promise mom and dad to take out the 2 trash, it is enough to take out the trash 1 once.

Score: 1

As an example we have two interfaces and 5 both have same function PrintIt and class 4 MyClass implements both interfaces.

interface IAInterface
{
  void PrintIt(string s);
}

interface IBInterface
{
  void PrintIt(string s);
}

class MyClass : IAInterface, IBInterface
{
  public void PrintIt(string s)
  {
    Console.WriteLine(s);
  }  
}

You can 3 refer to multiple interfaces through

  1. class object
  2. reference of IAInterface interface
  3. reference of IBInterface interface

    MyClass _class = new MyClass();
    _class.PrintIt("Class"); 

    IAInterface _aInterface = (IAInterface)_class;
    _aInterface.PrintIt("IAInterface");

    IBInterface _bInterface = (IBInterface )_class;
    _bInterface.PrintIt("IBInterface");

You 2 can also provide explicit Interface Implementation 1 ...

class MyClass : IAInterface, IBInterface
{
  public void IAInterface.PrintIt(string s)
  {
    Console.WriteLine("AInterface - {0}", s);
  } 

  public void IBInterface.PrintIt(string s)
  {
    Console.WriteLine("BInterface - {0}", s);
  }   
}
Score: 0

Since both methods have the same name and 3 the same signature (meaning same return 2 value type and same argument definition), it 1 is assumed that both methods are the same.

Score: 0

In multiple inheritance, the diamond problem 11 will occur when you use,

public class A 
{
    public virtual  void aMethod(); 
}
 public class B
{      
 public virtual void aMethod();
}

Now

public class aClass : A, B
 {      
public override void aMethod()  
    {       } 
}  

In the above 10 code, for aClass two vPtr will be created for 9 the same aMethod() in the vTable. Hence the ambiguity 8 occurs (diamond prob)

But when you are using 7 interfaces, no concept of vTable comes. Because 6 vTable is useful between base and derived 5 class scenario's in calling diff implementations 4 among them. In this case, the interface 3 doesn't gonna contain any implementation 2 and so no vPtr, vTable and hence no diamond 1 problem.

Score: 0

If you have to implement same named Methods 1 from different Interfaces. Try this.

interface IFoo
{
    void SayHello();
}
interface IBar : IFoo
{
    new void SayHello();
}
class myFooBarImp : IFoo, IBar
{
     void IFoo.SayHello()
    {
        Console.WriteLine("Hello from IFoo implementation");
    }

     void IBar.SayHello()
    {
        Console.WriteLine("Hello from IBar implementation");
    }

    public void SayHello()
    {
        Console.WriteLine("Hello from SayHello");
    }

}



 class Program1
{      
    public static void Main()
    {

        myFooBarImp obj = new myFooBarImp();
        obj.SayHello();
        (obj as IFoo).SayHello();
        (obj as IBar).SayHello();
        return;        

     }
}
Score: 0

Parent classes are designed to be consumed 6 by the child class while the Interface is 5 designed to be implemented by the child 4 class. Therefore even though both parent 3 interface has the same method, while inheriting 2 it won't create a confusion to the child 1 class.

Reference : https://dotnettutorials.net/lesson/multiple-inheritance-csharp/

More Related questions