Reputation: 2775
Currently I have the following:
public class ChildClass : ParentClass
{...
ParentClass implements an interface as follows (I need ParentClass to be instantiated and therefore can't be abstract):
public class ParentClass : IParentClass
{...
I also want child class to implement an interface so that I can mock this class out, but I want the inherited members of ParentClass to be visible to the ChildClass Interface.
So if I have a method MethodA() in the parent class, I want this method to be able to be called when using IChildClass and not just ChildClass.
The only way I can think of is to override the method in the ChildClass, define that method in IChildClass and the just call base.MethodA() but this doesnt really seem correct
Upvotes: 1
Views: 2500
Reputation: 3256
If I understand you correctly, you're saying that you want an inheritance hierarchy in your interfaces as well as your classes.
This is how you'd achieve such a thing:
public interface IBase
{
// Defines members for the base implementations
}
public interface IDerived : IBase
{
// Implementors will be expected to fulfill the contract of
// IBase *and* whatever we define here
}
public class Base : IBase
{
// Implements IBase members
}
public class Derived : Base, IDerived
{
// Only has to implement the methods of IDerived,
// Base has already implement IBase
}
Upvotes: 4
Reputation: 630
I think you can do two things.
1) You can inherit multiple Interfaces. C# supports this. You can only inherit from one base class, but you can inherit from multiple interfaces.
2) You can make your Interfaces inherit from each other. The IChildClass interface can inherit from the IParentClass interface.
Does that help?
Upvotes: 3