2

I have an an interface which uses an abstract class in its signature. My implementations then use the a subclass of the abstract class in their signature but this is not allowed. Can someone help me out? All I can find on the googles is generic interface and abstract class implementations...

public MyAbstractClass {
    abstract public void myMethod();
}

public MySubClass : MyAbstractClass {
    override public void myMethod(){
        ...
    }
}

And then I have my interface/implementation..

public interface MyInterface {
    MyAbstractClass myInterfaceMethod(MyAbstractClass blah);
}

public MyImplementation : MyInterface {
    MySubClass myInterfaeMethod(MySubClass blah){
        ...
    }
}

But I get an error building saying myInterfaceMethod doesn't implement the interfaces method...

Help?

Peter Vanleeuwen
  • 327
  • 3
  • 10

1 Answers1

6

You can't do that because it violates the interface. You can however make MyInterface generic.

public interface MyInterface<T> where T: MyAbstractClass {
    T myInterfaceMethod(T blah);
}

public MyImplementation : MyInterface<MySubClass> {
    MySubClass myInterfaeMethod(MySubClass blah){
        ...
    }
}
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445