11

I understand that C# does not support multiple inheritance, and that the solution is to use interfaces instead. But what I don't understand is why interfaces doesn't create the diamond problem the same way as multiple inheritance would. How does using interfaces avoid the pitfalls of multiple inheritance?

6
  • 2
    Sorry, but your question is not clear. Can you tell about your problem more clearly? Commented Mar 2, 2011 at 7:28
  • what is this "MI" you speak of? Commented Mar 2, 2011 at 7:29
  • 1
    @RPM1984: Probably multiple inheritance (not multiple interfaces as in the title) Commented Mar 2, 2011 at 7:30
  • ...and solution is that C# provides interfaces - this is not multiple inheritance, it is multiple implementation. There is a wealth of c# multiple inheritance questions already covered on SO, try this search. Commented Mar 2, 2011 at 7:44
  • The term "inheritance" includes two somewhat-orthogonal concepts: extensibility and substitutability. Interfaces provide the latter without the former; composition provides the former (if clumsily) but not the latter. Commented Mar 3, 2011 at 15:51

7 Answers 7

23

One class may implement any number of interfaces, even if those interfaces extend other interfaces as well. Multiple inheritance is not possible only with classes.

// This is not allowed
class A { void A() {} }
class B { void B() {} }
class C : A, B {}

// This is allowed
interface IA { void A(); }
interface IB { void B(); }

class A : IA, IB
{
    public void A() {}
    public void B() {}
}

The diamond problem exists with classes because there is a possibility of clashing implementations (if A and B have the same method and C extends both, which method does it take?). Interfaces, on the other hand, simply require an implementing type to have the methods that they declare.

If the exact same method is defined in two interfaces, and a class implements both interfaces, that doesn't matter. All the class needs to do is provide an implementation for the method so that code can be written to call that method. Meaning, this works:

interface IA { void Method(int x); }
interface IB { void Method(int x); }

class A : IA, IB
{
    public void Method(int x)
    {
        Console.WriteLine(x);
    }
}

Note that a class may still inherit from one other class, plus any number of interfaces:

class A : B, IA, IB {}
Sign up to request clarification or add additional context in comments.

6 Comments

How did you even figure out what he was asking!? :)
@tbridge: It was just a wild guess.
The downvote wasn't from me, but I can understand it. Your answer doesn't really touch on the diamond problem, which has nothing to do with methods whose names coincidentally match. It would be better to show two interfaces which implement a common IBase, and a class which implements both interfaces. The reason the diamond problem doesn't exist with interfaces is that if e.g. a class MyThing implements IList<T> and ICollection<T>, both of which inherit IEnumerable<T>, the class only implements IEnumerable<T> once. Whether it's cast directly to IEnumerable<T>...
A similar problem to the diamond problem shows up when you try to implement two interfaces with a common method - the method name may mean different things in the context of each interface.
You can also implement the methods explicitly. This allows you to provide different implementations for the methods of the different interfaces. Accessed through the interfaces, the correct methods will always be called.
|
4

The "diamond problem" is not present when just using interfaces because there is no ambiguous implementation possible. Read the Wikipedia article, which contains a full explanation.

Comments

2

Multiple interfaces will not create the diamond problem because each class must provide their own unique implementation, which means there is no sharing of resources.

C# does not allow multiple inheritance because they care about you, and made the language as shoot-yourself-in-the-foot-proof as possible.

1 Comment

'because they care about you' implies that other languages 'do not care' which is non sense :)
1

I think it is not fair to think about this in scope of C# only.

CLR itself does not support multiple inheritance. May be because they wanted to support other languages that do not support it currently or cannot support it in future.

2 Comments

Though that may be the case and something worth keeping in mind, I don't think it answers the question. I would have posted this as a comment, instead.
@Cody Gray: He wants to know why C# does not support it. And this is my answer for that.
0

If you ask about inheritance: class can inherit only one class, but can implement any number of interfaces. See this article about inheritance and polymorphism in C#.

Comments

0

This is a possible way to try to achieve something that you usually can achieve with multiple inheritance.

interface IA
{
    void DoAnything(string name);
}

class A : IA
{
    public void DoSomething()
    {
        // some code
    }
    public void DoAnything(string name)
    {
        // Some code
    }
}

class B : IA
{
    public void DoNothing()
    {
        // Some code
    }

    public void DoAnything(string name)
    {
        // Some code
    }
}

class StorageCache : IA
{
    private A ObjA;
    private B ObjB;

    public void DoAnything(string name)
    {
        ObjA.DoAnything(name);
        ObjB.DoAnything(name);
    }
    public void DoNothing()
    {
        ObjB.DoNothing();
    }
    public void DoSomething()
    {
        ObjA.DoSomething();
    }
}

Comments

0

You can inherit only one class and any number of interfaces at a time

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.