0

For example I have the code bellow:

public interface IFoo1
{
    void Foo1();
}

public interface IFoo2
{
    void Foo2();
}

public interface IOne : IFoo1
{
    void One();
}

public interface IFooList : IFoo1, IFoo2
{

}

public interface ITwo : IOne, IFooList
{

}

public class Test : ITwo
{
    public void Foo1()
    {
    }

    public void One()
    {
    }

    public void Foo2()
    {
    }
}

Interesting that class ITwo inherits IFoo1 twice(from IOne and from IFooList) Is it a bad practice ? I use these titles just for simplifying. But I have the same inheritance hierarchy in my prod code. And is it serious problem to have such type of inheritance ?

isxaker
  • 8,446
  • 12
  • 60
  • 87
  • 2
    Why wouldn't `ITwo` just inherit from `IOne` and `IFoo2`? `IFooList` has nothing in it – maccettura Feb 16 '18 at 15:07
  • 3
    This is more of a code review question than a SO question, but it's sometimes necessary. It is, however, a little code-smelly. Typically, when you find yourself with inheritance loops, it's time to reconsider your code structure and see *why* it's looping. – CDove Feb 16 '18 at 15:07
  • 1
    It depends. Do you really want to use `IFooList` classes as if they were IFoo1 or IFoo2 or was this a quick & dirty way to add Foo1 and Foo2 to some classes? And why doens't `ITwo` inherit from theIFoos. If there *is* an `is-a` relation it's not *necessarily* bad. Otherwise it's very bad - quick&dirty fixes are typically very dirty – Panagiotis Kanavos Feb 16 '18 at 15:07
  • @maccettura I have more complicated inheritance hierarchy. it's just example to explain the problem. – isxaker Feb 16 '18 at 15:09
  • Interfaces are *interfaces*, in the way headphone sockets, wall sockets, USB sockets are interfaces. If you want multiple adapters on a device, you add multiple adapters to the device. You don't try to create one adapter that will fill accept any cable, any voltage, any signal – Panagiotis Kanavos Feb 16 '18 at 15:09
  • @CDove thanks. I got your point – isxaker Feb 16 '18 at 15:10
  • @PanagiotisKanavos I very appreciate your comment. It helps me. Thanks a lot. – isxaker Feb 16 '18 at 15:11

1 Answers1

1

There is a flaw in your inheritance chain. This can be more easily observed if we apply some names that make sense.

Your code in its current form:

public interface IAnimal
{
    void Breathe();
}

public interface ILegged
{
    void Stand();
}

public interface IFlyingAnimal : IAnimal
{
    void Fly();
}

public interface ILeggedAnimal : IAnimal, ILegged
{

}

public interface IBird : IFlyingAnimal, ILeggedAnimal
{

}

public class Eagle : IBird
{
    public void Breathe()
    {
        throw new NotImplementedException();
    }

    public void Stand()
    {
        throw new NotImplementedException();
    }

    public void Fly()
    {
        throw new NotImplementedException();
    }
}

As you can see, IBird is both an IFlyingAnimal and an ILeggedAnimal, which is fine from a compiler perspective, but there is overlap because they are both an IAnimal.

What you need, apparently, is an IFlyingAnimal that is ILegged:

public interface IBird : IFlyingAnimal, ILegged
{

}

That will give you a proper chain of inheritance.

You now have and Eagle that is an IBird, which is an IFlyingAnimal that is ILegged. It can Breathe, Stand in its legs and Fly.

JuanR
  • 7,405
  • 1
  • 19
  • 30