开发者

Interface and its accessibility

I have been asked a question in an interview about interfaces. I am not sure whether it's really possible. Please see the question below.

There are 3 interfaces A, B, and C. A inherits from interfaces B and C:

public interface A : B,C
{

} 

We have to make sure that users of this interfaces can't use B and C directly or independently and have to use only A.

I could think of the following scenarios:

  1. Make B and C inner interfaces. But I don't see any real use as I could directly define all the members in A itself.
  2. Make B and C private interfaces. But how can I make it? Also, I have seen at the below MSDN link that B and C have to be at least as accessible as A:开发者_StackOverflow中文版 http://msdn.microsoft.com/en-us/library/aa664578%28v=VS.71%29.aspx.

Is there any way to do this or is the question itself wrong?


What you are asking for would be a violation of the Liskov Substitution Principle.

If A implements B this way, it should always be usable directly as a B. Trying to prevent this would be violating one of the main precepts of object oriented design.

I suspect the interviewer was trying to see if you understood this core concept, and would say "This is wrong because ..." right from the start.


Seems to me like that's a bad design pattern from the get-go. If users are required to inherit both B and C, as you said, it make more sense to just create the one interface. Secondly, B and C must be at least as accessible as A. (You can't make them internal or private if A is public).

I think the only real solution is to combine the interface definitions into A, rather than having the three seperately.


We have to make sure that users of this interfaces can't use B and C directly or independently and have to use only A.

You don't need to obscure B or C to force someone to use just A, wherever you are using B or C and want to wait just A, please change it to A.

hope it helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜