-3

Here's my code sample in C#:

abstract class A {
    public interface IA{

    }

    abstract protected void Print(IA obj);
}

abstract class B : A {
    public interface IB : IA{

    }

    override protected void Print(IB obj){
      // do something else
    }
}

Apparently, the compiler is not crazy about me overriding the Print method for the class B.

I am getting "no suitable method found to override."

Is there a way to make it work? I am not looking for a design change, but a technical solution.

Johnny Wu
  • 1,297
  • 15
  • 31

2 Answers2

1

You can't change the method signature when you override it, and that's what you are doing by changing the argument type from IA to IB.

Lucky for you, you can use generics to solve this:

public interface IA{

}

public interface IB : IA{

}

abstract class A<T> where T : IA 
{
    abstract protected void Print(T obj);
}

abstract class B : A<IB> {

    override protected void Print(IB obj){
      // do something else
    }
}
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • This is the kind of suggestion I am looking for. However, the compiler doesn't see IB when I define the class B or I am missing something? – Johnny Wu Jul 24 '18 at 19:02
  • Did you try it and got a compiler error? I wrote the code directly here, so I might be wrong, however as far as I remember that's the way to handle such cases... – Zohar Peled Jul 24 '18 at 19:04
  • Yup, it's saying "The type or namespace name 'IB' could not be found (are you missing a using directive or an assembly reference?" I even copied & pasted the name to make sure I didn't have a spelling error. :) – Johnny Wu Jul 24 '18 at 19:07
  • probably because it's an inner type of the class. What if it would be a top level type? – Zohar Peled Jul 24 '18 at 19:11
  • yeah...it doesn't quite make sense to encapsulate the interfaces w/ the classes in this case. I am separating the interfaces from the classes...Thanks for the suggestion. – Johnny Wu Jul 24 '18 at 19:13
  • After the separation, your suggestion works. Giving you credit for it. – Johnny Wu Jul 24 '18 at 19:42
  • Glad to help :-) – Zohar Peled Jul 24 '18 at 19:43
  • @JohnnyWu I've updated the code in my answer to reflect that. Would you mind taking a look to see that I didn't miss anything (and fix if I did), so that future readers might also benefit from this answer? – Zohar Peled Jul 24 '18 at 19:47
  • confirmed...that's exactly what I have...I am not sure if you are getting the credit, but an unkind marked my question as duplicate which. I'll reserve my comment about that. If you don't get the credit, I am sorry. – Johnny Wu Jul 24 '18 at 19:50
  • @JohnnyWu credit is not that important, but yes, I do get it even if the question is a duplicate. It's impossible to add new answers to a closed question, but every existing answer is just like on an open question. – Zohar Peled Jul 24 '18 at 19:52
0

This is very weird design, but you can achieve this by:

   abstract class A {
    public interface IA{

    }

    abstract protected void Print(IA obj);
}

abstract class B : A {
    public interface IB : IA{

    }

    override protected void Print(IA obj){
    }

    protected void Print(IB obj){
      Print(obj as IA);
    }
}
Krzysztof Skowronek
  • 2,796
  • 1
  • 13
  • 29