Can a child class implement the same interface as its parent?
Yes, you can explicitly redeclare that you want to implement IDispatch
, and implement it explicitly again in Bar
.
However, you won't be able to call the original implementation in Foo
. If you need to do that, you'll need to change Foo
either to use implicit interface implementation with a virtual method (which can be overridden and then called with base.Dispatch()
in Bar
) or make the Foo
implementation call a protected virtual method which again you'd override in Bar
.
You could, alternatively, do this one of two ways:
First, don't implement the interface explicitly:
public class Foo : IDispatch {
public virtual void Dispatch() {
whatever();
}
}
public class Bar : Foo {
public override void Dispatch() {
whateverElse();
}
}
Second, implement it explicitly but add a function that the child class can override:
public class Foo : IDispatch {
void IDispatch.Dispatch() {
this.Dispatch();
}
protected virtual void Dispatch() {
whatever();
}
}
public class Bar : Foo {
protected override void Dispatch() {
whateverElse();
}
}