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:
- Make
BandCinner interfaces. But I don’t see any real use as I could directly define all the members inAitself. - Make
BandCprivate interfaces. But how can I make it? Also, I have seen at the below MSDN link thatBandChave 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?
What you are asking for would be a violation of the Liskov Substitution Principle.
If
AimplementsBthis way, it should always be usable directly as aB. 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.