How does using interfaces overcome the problem of multiple inheritance in C#?

不羁岁月 提交于 2019-11-28 09:13:08

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 {}

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.

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.

You can inherite only one class and unlimited number of interfaces at one time

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.

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#.

Fun Mun Pieng

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();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!