What would you want the behavior to be? Whenever you call IFoo.Bar()
its going to use the definition in IBar, because its just an interface and has no separate conception of Bar()
whatsoever. You can only have a different method called when casting to a superclass with the new
keyword, and that's when you are overriding a method in a class, not implementing and interface.
When interfaces inherit one another, sub-interfaces have little ownership conception of a method. Its as if the sub-interface has both methods declared in it.
Caution: potential over-complication and brain meltdown!!! Read with caution!!!
I believe this is allowed, correct me if I'm wrong:
public class IBar {
virtual void Bar() {
//IBar implementation of Bar
}
}
public class IFoo: IBar {
new virtual void Foo() {
//implementation of Foo when currently casted to IFoo
}
}
public class FooImpl: IFoo {
new void Foo() { /* implementation of Foo when cast to FooImpl */ }
new void Bar() { /* implementation of Bar when cast to FooImpl */ }
}
left the I's before the class name for clarity, but there are no longer any interfaces. The method that is called will depend on what class the object has been cast to.
IBar b = new IBar();
b.Bar(); //calls IBar.Bar
IFoo f = new IFoo();
f.Bar(); //calls IFoo.Bar
f.Foo(); //calls IFoo.Foo
IBar fooAsBar = (IBar) f;
fooAsBar.Bar(); //calls IBar.Bar
FooImpl fi = new FooImpl();
fi.Bar(); //calls FooImpl.Bar
fi.Foo(); //calls FooImpl.Foo
IFoo fooImplAsFoo = (IFoo) fi;
fooImplAsFoo.Bar(); //calls IFoo.Bar
fooImplAsFoo.Foo(); //calls IFoo.Foo
IBar fooImplAsBar = (IBar) fi;
fooImplAsBar.Bar(); //calls IBar.Bar
Oh, and you're not using nested interfaces, they are just inheriting from one another. Nested interfaces are like this:
interface IBar {
void Bar();
interface IFoo {
void Foo();
}
}
As you can see this is completely different. The relationship between the two interfaces is only that one can only be used inside of the other. They are a complicated and somewhat tricky topic. You can read more here. :D