Can we implement interface without overriding some of its method.I heard this is possible in some cases
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()");
}
}
精彩评论