Search code examples
c#.netdesign-patternsclass-design

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: 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?


Solution

  • 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.