开发者

Can we implement interface without overriding some of its method.I heard this is possible in some cases

开发者 https://www.devze.com 2023-01-17 11:43 出处:网络
Can we implement interface without overriding some of its method.I heard this is possible in some cases.I c开发者_运维问答ame across this question recently while interview.You can inherit the implemen

Can we implement interface without overriding some of its method.I heard this is possible in some cases.I c开发者_运维问答ame across this question recently while interview.


You can inherit the implementation from a base class, if that's what you were thinking of:

interface IFoo
{
    void Bar();
}

class Parent
{
    public void Bar() {}
}

class Child : Parent, IFoo {}

If that's not it though, I don't know what you're thinking of.


You can implement an interface in an abstract class, and leave some parts of the interface abstract to implement them in further-derived classes:

interface A
{
    void f();
    void g();
}

abstract class B : A
{
    public void f()
    {
        Console.WriteLine("B.f()");
    }

    public abstract void g();
}

class C : B
{
    public override void g()
    {
        Console.WriteLine("C.g()");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消