Q1: What is the difference in philosophy while designing OOP code using "interface inheritance" and "composition with interface"? (Please focus on dealing with interface part ,as I am aware of inheritance and composition and why composition is favored over inheritance).
Q2: Any use case scenario when interface inheritance should be favored over composition or composition+interface?
P.S. Focus should be in the role of interface in inheritance and composition.And any addition cue dealing with related design will be much appreciated.
You can say that interface inheritance is declarative, because it only states what interfaces should look like:
public IDerivedInterface : ISomeOtherInterface { /*...*/ }
These interfaces still have no behaviour, only shape.
Composition with interfaces, on the other hand, is a way to assemble implementations.
public Foo DoFoo(Bar bar)
{
var qux = this.baz.Corge(bar); // baz is IBaz, an interface
var garply = this.grault.Waldo(qux); // grault is IGrault, another interface
return garply.ToFoo();
}
This DoFoo
method composes IBaz
and IGrault
. One, or both, of these interfaces can be defined as stand-alone interfaces, or intherited interfaces - the DoFoo
method doesn't care.
As the above answer explains, interface inheritance and composition are two independent concerns, so it makes no sense to favour one over the other. They can coexist, but they don't have to.