2

Let's say we have two interfaces:

interface Ilayer1 : Ilayer2 { string testMethod1(); } interface Ilayer2 { string testMethod2(); } class A: Ilayer1 { ...//implement all methods of Ilayer 1 and Ilayer2 } 

So my questions are:

  1. What is the purpose of interface inheritance? Since they can have different method signatures to be implemented, so what does Ilayer1 get by inheriting from Ilayer2?
  2. We can simply remove the inheritance relationship between Ilayer1 and Ilayer2 and just let class A implement both interfaces as class A: Ilayer1, Ilayer2
3
  • 1
    IPrimate : IMammal : IVertebrate for example. Each of those add specific characteristics to what it inherits that others may not. For example you could also have IFish : IVertebrate since fish and mammals have some distinct separate properties. Commented Mar 13, 2019 at 23:39
  • So you are OK with duplicating over and over again Ilayer2 in each implementer of Ilayer1? Commented Mar 13, 2019 at 23:43
  • If ILayer1 adds functionality to ILayer2, or ILayer2 is something that ILayer1 also makes use of (like IComparable) then it makes perfect sense. Real-world examples would make it more obvious. Commented Mar 13, 2019 at 23:55

3 Answers 3

4

It's just the same semantics as inheritance between classes.
In the OOP world, A derived type Inheriting a base type when it's a more specific version of the base type - whether this type is an interface or a class the principle stays the same.

Here's an example from the .Net framework: The IList interface inherits the ICollection interface which inherits the IEnumerable interface.

The IEnumerable interface provides the GetEnumerator() method which is needed for enumerating using the foreach loop.

The ICollection Adds new capabilities: the Count property and the CopyTo method.

The IList adds even more capabilities - the indexer, the Add and Remove methods and so on.

So an IList is a more specific type of an ICollection, which is a more specific type of an IEnumerable.

The fact that interfaces can inherit one another means that you can have a polymorphic point of view also on interfaces and not only on classes - which can help simplify your code very much when dealing with multiple interfaces inheriting each other.

One more benefit of this is the fact that you can declare extension methods on an interface, and use it on any class that implements this interface whether directly or indirectly by implementing an interface that inherits it - just like in classes.

As for the question of why not remove the inheritance and implement separately the two interfaces - that would make sense if the interfaces was unrelated - like, for instance, in the Control class (in System.Windows.Forms) - It implements many interfaces, like IDropTarget and IComponent which are unrelated.

Sign up to request clarification or add additional context in comments.

Comments

2

One real life example:

interface IShape { double X { get; } double Y { get; } } interface IAreaShape : IShape { double GetArea(); } interface IPerimeterShape : IShape { double GetPerimeter(); } 

Now assume you have a Rectangle : IAreaShape, IPerimeterShape shape, and you can calculate both Area and Perimeter of it. When you have an object of type IAreaShape or IPerimeterShape, it has to be a IShape. This answer for your 2nd question.

Now for your first question, just assume, for the sake of example, we have a Circle that I can only calculate Area but not Perimeter. In this case, we simply declare it Circle : IAreaShape.

And for management: List<IShape> shapes can take both Circle and Rectangle and anything as long as they implement IShape (or whatever derived type of IShape). You can do this if you don't care if their area or perimeter can be calculated, but you can always get their X and Y coordinates.

Comments

0
  1. Ilayer1 inherits Ilayer2 so that means that anyone implementing Ilayer1 will need to implement testMethod1() and testMethod2(). Why have it? Why not? Because we can? I think in large projects with lots of interfaces it can become pretty tedious if you need to specify all interfaces some class is implementing separately (though in good SOLID design a class usually shouldn't implement more than a few interfaces anyway). So you can "group" interfaces by having them implement others. You can also 'version' an interface by using interface inheritance.
  2. You can. It's up to you. It's good to have choices. Use inheritance or implement the interfaces separately. It's up to you.

Interfaces can inherit from other interfaces. A class might include an interface multiple times through base classes that it inherits or through interfaces that other interfaces inherit. However, the class can provide an implementation of an interface only one time and only if the class declares the interface as part of the definition of the class (class ClassName : InterfaceName). If the interface is inherited because you inherited a base class that implements the interface, the base class provides the implementation of the members of the interface. However, the derived class can reimplement any virtual interface members instead of using the inherited implementation. Source

Comments