Interface is lost with inheritance. How to solve?

I have an abstract class Skill, from which Character Skill and NPC skill inherit.

I make an interface that requires a TestSkill (Skill skill)
But it does not work if then I do TestSkill(Character Skill).

How do i solve this?

This is a matter of covariance / contravariance. Imagine this

public interface IMyInterface
{
    TestSkill (Skill skill);
}

public class SomeClass : IMyInterface
{
    public TestSkill(Character Skill)
    {
    }
}


IMyInterface test = new SomeClass();
test.TestSkill(someNonCharacterSkill);

As you can see we create a SomeClass instance which implements our interface. Since the TestSkill method of the interface accepts any “Skill” derived class it can’t work with a method which only accepts a Character derived class. The interface however allows passing any Skill. So your TestSkill method does not match the interface definition. You can not assign match a more derived method parameter with a less derived definition from the interface. It only works the other way round. If the interface would require a Character than a class implementing the interface could accept a Skill as parameter since Character is derived from Skill it doesn’t matter.

Since your problem description doesn’t include any additional information we can’t say more about that besides that it’s not possible. We have no idea for what you want to use that interface. Your question is too abstract.

Since the CharacterSkill class is derived from the Skill class, you should be able to CAST a CharacterSkill reference into a Skill reference; I’m actually surprised it didn’t do this cast automatically.

Here is a manual-cast usage example:

referenceToClassImplementingISkillInterface.TestSkill( (Skill) someCharacterSkillReference);

[Or perhaps you are talking about defining the function, rather than invoking it? In which case… see bunny’s answer- but let me add: I suspect the design-issue may be in requiring a parameter at all. If the TestSkill function is to be defined INSIDE Character AND NPC class definitions (two that contain Skills, but are NOT derived from the Skill class), the function body itself should probably be extracting the appropriate data to “test”. ]