2

I have a problem that I have two types, and the difference is just in a signiture of a method. My 'Plan-B' is that forget this difference and I handle it in the concrete implementation, but for 'Plan-A' I want the following:

public interface MyInterface
{
    int property;
    void MyMethod();
    ICollection<int> MyMethod();
}

public interface A : MyInterface
{
    void MyMethod();
}

public class AClass : A
{
    public void MyMethod() { ... }
}

public interface B : MyInterface
{
    ICollection<int> MyMethod();
}

public class BClass : B
{
    public ICollection<int> MyMethod() { ... }
}

So I want that AClass has to be implement methods from A, and do not have to implement ICollection MyMethod from MyInterface.

I hope I wrote it clear.

Is it possible?

Thank you!

3
  • 2
    Why must A and B extend MyInterface then? Commented Nov 5, 2011 at 17:48
  • Because they had several common properties and methods. Commented Nov 5, 2011 at 17:50
  • Why have 3 interfaces when 2 will do? Are these interfaces intended to be much more complicated and intertwined than you describe here? It looks to me (and after a +1 to @BoltClock, I would say to him too) that you could do just fine with having A and B stand on their own. Commented Nov 5, 2011 at 17:51

5 Answers 5

3

Not sure I understand precisely, but try this:

public interface MyInterface
{
    int property;
}

public interface A : MyInterface
{
    void MyMethod();
}

public class AClass : A
{
    public void MyMethod() { ... }
    int property;
}

public interface B : MyInterface
{
    ICollection<int> MyMethod();
}

public class BClass : B
{
    public ICollection<int> MyMethod() { ... }
    int property;
}

In other words, interface A adds the method void MyMethod() to interface MyInterface, and interface B adds the method ICollection<int> MyMethod() to interface MyInterface.

Note that you still won't be able to call e.g.

MyInterface object = new AClass();
object.MyMethod();

as MyMethod() won't be a member of MyInterface.

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

1 Comment

I tried but my problem comes when I want to use a code implemented for MyInterface and I can't cast between MyInterface and A, and MyInterface and B. I have to go now but I'll come back and try to explain it correctly. Thanks!
0

Simply remove the part where interface A and B implement MyInterface. What's the point of MyInterface if you state you don't want to use it?

Comments

0

Remember that interfaces are contracts to a concrete implementation and that for it to make sense architecturally, any and all implementations should logically make use of the outlined properties and functions in the contract. Having 3 interfaces will give you what you're looking for and make more sense than trying to stretch 1.

Comments

0

No, you can't do that. The return type is part of the method signature. So if you don't implement ICollection MyMethod() you're not fulfilling the contract defined by MyInterface. Why can't you simply implement MyMethod in AClass as returning null?

I feel you'r going against what polymorphism is there for. You want to be able to use the interface, and call polimorphically on the classes, but you want that to return different things. How do you plan to deal with the difference in returns? If the signature is different you have to manually adjust either before or after the call.

If you return a null from AClass then you can adjust after the call. And if you want to return a void, then I'd rather move the MyMethod out of MyInterface down to B and A, and do an AS A / B cast and do whatever you need to do in either case.

Comments

0

1) Use abstract class, so you can delegate implementation of particular method/property to the nested classes:

public abstract class AClass : A
{
    public void MyMethod() { ... }
    public abstract ICollection<int> MyMethod();
}

2) Or extract ICollection<int> MyMethod() in another interface which is not part of A inheritance tree

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.