4

I have a problem with using inherited interface. I will explain my problem on example below. Let's say I have Interface IFlyable:

public interface IFlyable
{
    IVerticalSpeed Speed { get; set; }
}

It contains IVerticalSpeed interface. I created another interface called ISpeed which inherits from IVerticalSpeed interface:

public interface ISpeed : IVerticalSpeed
{
    int MaxSpeed { get; set; }
}

In next step I created a class Fly which implement IFlyable interface:

public class Fly : IFlyable
{
    public IVerticalSpeed Speed { get; set; }
}

Everything is fine... but what if I wanted to replace IVerticalSpeed interface to ISpeed interface which inherits from IVerticalSpeed interface?

public class Fly : IFlyable
{
    public ISpeed Speed { get; set; }
}

I thought that everything should be fine because my ISpeed interface is IVertialSpeed interface + anything that ISpeed interface contatins. But that is not right. I get error which says: "Fly does not implement interface member IFlyable.Speed (...). Why?

4
  • 2
    you dont show IVerticalSpeed Commented Feb 28, 2017 at 13:58
  • 1
    Possible duplicate of Why can't C# interfaces contain fields? Commented Feb 28, 2017 at 14:00
  • @Seabizkit - No, but that shouldn't matter. interface IVerticalSpeed {} is enough. Commented Feb 28, 2017 at 14:03
  • at Adam, no , more info helps. I cant not see if IVerticalSpeed has IVerticalSpeed as a property.... odd yes but so is the question, so asking people to help with limited info doesn't paint the whole picture. So the answer would be then that Fly no longer implements IVerticalSpeed as a property as denoted by 'IFlyable' Commented Feb 28, 2017 at 14:13

5 Answers 5

4

All answers show you possible solutions but none actually answer the important question here:

I thought that everything should be fine because my ISpeed interface is IVertialSpeed interface + anything that ISpeed interface contatins. But that is not right. I get error which says: "Fly does not implement interface member IFlyable.Speed (...). Why?

You've said it yourself. ISpeed is an IVerticalSpeed, but not all IVerticalSpeeds are ISpeeds so you are not satisfying the contract at all.

What would happen if your code was allowed and I wanted to do the following:

public interface IMyOtherSpeed: IVerticalSpeed { ... }
IFlyable myFly = new Fly();
IMyOtherSpeed mySpeed = new MyOtherSpeed();

myFly.Speed = mySpeed; //Runtime error, mySpeed is not an ISpeed?!?

You see the issue now? You'd be breaking the interface's contract, becuase your class only accepts ISpeed when it should accept any IVerticalSpeed.

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

1 Comment

Another way to put the emphasis: the contract is broken by the set, the OP was reasoning about the get part.
2

If you use generics you can do something like

public interface IFlyable<out T> : where T IVerticalSpeed
{ T Speed {get;set;} }

Then in your class you would do something like

public class Fly : IFlyable<SomeSpeedClass>
{
  public SomeSpeedClass Speed{get;set}
}

3 Comments

Do you think of an airplane as an airplane of vertical speed? Just because its possible to do with generics doesn't mean it should. This particular example seems like a bad fit for genercis, to be honest.
@InBetween what would the best approach for this? Because the Fly class has to implement IFlyable interface but he wants an instance of ISpeed returned in the Speed property instead of an IVerticalSpeed instance.
Without context I wouldn't be able to answer what the best approach is. But its reasonable to expect that IFlyable will have similar properties for HorizontalSpeed, HorizontalAcceleration, VerticalAcceleration, etc. Are you going to create an IFlyable<T1, T2, T3, etc> interface? And lets not even get started on how to deal with generic type variance... it all gets very messy very fast. Generics shouldn't be a solution just to make the type system work.
1

Because your property's signature must stay 100% the same. You can implement a new property, as a workaround. But it has it's limitations, too.

You can do this if your inheritance would work both ways. You already know, all ISpeeds are IVerticalSpeeds. But that's not the case the other way round. Not all IVerticalSpeeds are ISpeeds. So we can give your class another method, but it will face a problem at exactly that point:

  public interface IVerticalSpeed
  {
    int Value { get; set; }
  }

  public interface IFlyable
  {
    IVerticalSpeed Speed { get; set; }
  }

  public interface ISpeed : IVerticalSpeed
  {
    int MaxSpeed { get; set; }
  }

  public class Fly : IFlyable
  {
    public ISpeed Speed { get; set; }

    IVerticalSpeed IFlyable.Speed
    {
      get
      {
        return this.Speed;
      }
      set
      {
        // Wow, wait, you want to SET an IVerticalSpeed,
        // but not every IVerticalSpeed is an ISpeed... what now?
        // this.Speed = value;
      }
    }
  }

This is not a problem of code, this is a design problem. What do you want to do, if someone sets an IVerticalSpeed to your Fly? Because your interface says that's allowed. Maybe you don't want setters?

Comments

1

Instead of

public class Fly : IFlyable
{
    public ISpeed Speed { get; set; }
}

You should match the IFlyable interface

public class Fly : IFlyable
{
    public IVerticalSpeed Speed { get; set; }
}

Interfaces are not inheritance, must be matched exactly.

Edit: Let me rephrase, interfaces that implementing other interfaces is not inheritance, hence the error. Interfaces must me matched exactly, that's is why it raises the error (ISpeed != IVerticalSpeed)

1 Comment

upvoting @InBetween because it is more clear, although both answers try to answer the same.
0

You haven't shown IVerticalSpeed, but if you have control of it consider

public interface IFlyable<out TSpeed>
    where TSpeed : IVerticalSpeed
{
    TSpeed Speed { get; set; }
}

then implemented in Fly like this

public class Fly : IFlyable<ISpeed>
{
    public ISpeed Speed { get; set; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.