This is very basic question of interview that can we declare a method as private inside interface and my answer is simple in interface we have only public variable or methods and then next question comes up... why ?
-
6If it was private, how would you implement the interface? – Sayse Sep 02 '14 at 08:29
-
1Can't the members and methods be protected/packaged ? Private would mean they cannot be implemented so this is probably why it's illogic, but about protected and package this can have sense. – Hybris95 Sep 02 '14 at 08:30
-
possible duplicate of [Why should we declare interface methods as public?](http://stackoverflow.com/questions/9614708/why-should-we-declare-interface-methods-as-public) – Dimitris Kalaitzis Sep 02 '14 at 08:30
-
2@Hybris95: What would be the value of enforcing a protected interface? – Oliver Charlesworth Sep 02 '14 at 08:31
-
You define an interface for others to use it, what they couldn't if it was private. – Theolodis Sep 02 '14 at 08:31
-
Interfaces are only "contract" - every class that implements some interface, must implement methods required by interface. On the other hand, interface doesn't care about way that methods are implemented - so it doesn't need to hold any private values. If interface should know sth about implementation (f.e. common method for each implementation) you should consider `abstract class` (but remember - in abstract class you loose multi-interihance). In some programming languages there are interfaces with dofault method implementations (f.e java 8). – Sep 02 '14 at 08:32
-
2@DimitrisKalaitzis Remember we are talking about C# specs and not Java here. – Hybris95 Sep 02 '14 at 08:33
-
@OliCharlesworth There is no value added with the protected keyword, so as the public by the way ;) – Hybris95 Sep 02 '14 at 08:35
-
I think the only possible answer to this question is "Because that is how they were implemented." Maybe, with the addition that public members represent the most common use case. At least `internal` interface members are thinkable, but they are simply not a part of C# (or the CLI). – O. R. Mapper Sep 02 '14 at 08:37
4 Answers
Interface has a semantic of a public contract provided by some implementation. It is like the API which class (or globally modules, subsystems, components, ...) privides to its clients. Private methods are used to handle some inner logic and thus depend on what actually implementation is. It is context-dependent. So, if class client needs to call private method, than 1) it must be public or 2) there is a bad design practice.
That's why as an abstraction interface can be considered as a fundamental basis of OOP, it is very important instrument for realization of the things like abstraction, encapsulation and polymorphism.

- 1,131
- 9
- 18
There are various interpretations of how interface members could have different visibilities. While there are probably more conceivable levels for visibility of identifiers, let us just have a look at those available in C#, with some thoughts of whether they could be meaningfully integrated into interfaces.
1) Interface members are only visible to code outside of the interface based on the rules of the respective visibility level.
public
: Interface members in C# are public by default, so this works.internal
: If single interface members could be declared asinternal
, it would mean that a part of the interface could only be implemented by classes from the assembly that the interface resides in. A similar situation is already existing in cases where abstract class members or constructors of abstract classes are declared asinternal
- this effectively prevents subclassing outside of the assembly of the base class.
As for scenarios where this might be used, think of APIs that return objects that later get consumed again by said API. If, for some reason, the API can only consume objects that it instantiated itself, being able to declare the base class in a way that 3rd party code cannot derive its custom classes, but only use what it gets from the API, while not being able to do the same with interfaces feels oddly asymmetrical.protected
: Protected members are only visible from within their own class, and any of its subclasses. Applying the same assumptions as above, this would mean that only code within the interface declaration itself (well, an interface can contain almost no code where this could be relevant1), of derived interfaces (same as before), and possibly of classes implementing the interface could see those protected members.
The latter case is where things might become interesting, if only in rather contrived situations: A method within that class may want to consume anything that implements the interface, be it the current instance of the class itself, or any other type nested therein, while getting semi-exclusive access to the protected methods.private
: Everything withprivate
visibility is only visible to the declaring interface itself. As interface members can hardly1 be referenced from within the interface itself, this would probably not be very useful. However, there may still be corner cases, such asconst
definitions within an interface that are referenced in attributes in said interface, but should not be available to outside code - however, C# does not allow forconst
values in interfaces, even though a similar feature is not unprecedented in the CLI world.
2) Interface members must be implemented with (at least?) the visiblity specified in the interface and adhere to that visibility when accessing the member via a reference typed to the class, but are generally publicly visible when accessing the member via a reference typed to the interface.
Again, there would be no change in the case of public
members, while internal
, protected
and private
visibility would allow for interfaces that allow for an apparently partial implementation. A similar effect can currently be achieved by explicit interface implementation, so in some respect, this would be syntactic sugar to avoid writing the same method twice, once with the intended visibility, and once with the interface-specific explicit implementation declaration (even if the latter often only forwards calls).
3) For the sake of completeness, even though your question didn't sound like including this option: For each class, the visibility of the whole interface is determined by the implementor.
Like this, a class could implement an interface, but that fact would only be apparent within the respective visibility level, while outside, the class would simply not be considered assignment-compatible or castable to that interface.
This may be particularly beneficial if the interface itself needs to have a lesser visibility than the class, as the interface should be processed only internally and is meaningless to the outside world. Currently, this is not possible in C#; interfaces need to be at least as accessible as their implementing classes. But then, this can also be achieved by implementing an internal interface in a public class.
As there are so many ways that visibility of interface members could be feasible and, at least in some cases, useful, the most definitive answer to this question is probably "because the development team decided to specify C# this way".
1Presumeably, the nameof
operator of C# 6 could be used on a protected/private interface member in an attribute declaration of the same interface.

- 1
- 1

- 20,083
- 9
- 69
- 114
Interfaces are contracts that classes require to implement in order to ensure that the consumer of some given class will receive instances of a class which mandatorily implements an interface.
If interfaces would allow private members, the fact that you couldn't invoke interface members which are hidden from the public surface would defeat the purpose of an interface, because a consumer wouldn't be able to call these private members (so... why it would use an interface?).
For example, interfaces provide typing to objects. If some consumer code relies on receiving objects implementing some interface to avoid redundant dependencies on the assembly which uses it and the whole assembly has access to an interface which doesn't provide members publicly, how that consumer would work with the so-called object?
public interface IDoesSomething
{
private void Do();
}
public void SomeMethod(IDoesSomething some)
{
some.????? // <---- what? the object doesn't have public members!
}
In fact, since interfaces are just metadata, I understand that you might think "I would declare private members to force implementers to call some logic in an expected order", but again, since they're just metadata, you've no way to ensure that some public member will call a private member in the right order. And implementations wouldn't have access to interface private members because they would need to be protected for that matter (that's why abstract classes exist)...

- 63,804
- 18
- 124
- 206
-
2It could be logical to implement the members of an internal interface as internal methods/properties. Sadly, it is not the way the language is designed. – Ndech Sep 02 '14 at 08:42
-
@Ndech I would say that *internal interfaces* are possible. It's a required design decision: your interface is 100% public or 100% internal, because otherwise it would defeat the purpose of interfaces too. – Matías Fidemraizer Sep 02 '14 at 08:44
-
@MatíasFidemraizer: I can imagine interfaces that have both public and internal members. The internal members would be invisible to other assemblies (hence the interface could not be implemented there, similarly to when you give an abstract class only internal constructors). That could be an elegant way of restricting method arguments typed to such an interface type to classes declared by the assembly that also declares the interface. They would work analogously to internal members in classes. – O. R. Mapper Sep 02 '14 at 09:33
-
@O.R.Mapper Maybe you're right, but I don't see the point of an interface that mightn't be able to be implementable by any library. I mean, this isn't the purpose of interfaces. Am I wrong? If you use interfaces is because you want to publish some API so it provides default implementations but same provider might accept any implementation if it behaves like the consumer expects... – Matías Fidemraizer Sep 02 '14 at 09:37
-
@MatíasFidemraizer: You may also be using interfaces because your own API can return a variety of different internal classes under the hood. And users of your API may be expected to pass back such instances to other parts of your API. Parts that expect one of your own classes, rather than some other type - maybe to ensure certain invariants, such as serializability, which cannot be enforced by the type restriction on its own (?) Usually, such scenarios are solved with classes that have internal members, but then, being unable to do the same with interfaces feels asymmetric. – O. R. Mapper Sep 02 '14 at 09:57
-
@O.R.Mapper I understand your points. But how you would implement an internal member of some mixed public/internal interface from a third-party library? there's a big issue here in your argument (or maybe I'm missing some part of it...) – Matías Fidemraizer Sep 02 '14 at 10:05
-
@MatíasFidemraizer: The point in this use case is that third-party libraries are not allowed to implement the interface, they are just allowed to use instances that implement the interface that were supplied by your API, and to pass those instances back into your API. cf. abstract classes with internal constructors; third-party libraries are not allowed to derive new classes from those, either, they can just use the instances supplied by the implementing assembly. – O. R. Mapper Sep 02 '14 at 10:23
-
@O.R.Mapper I see, uhm........ Maybe this use case might be interesting. BTW I would say that a new modifier or attribute might be better than enforcing this constraint because the interface has internal members. What about `[MemberExtensibility(MemberExtensibilityMode.Internal)] public interface ISome`. Public, but not publicly implementable... – Matías Fidemraizer Sep 02 '14 at 10:29
-
@MatíasFidemraizer: Possibly - though this would be beyond the scope of this question/answer/discussion. I just wanted to point out that associating individual visibilities with members of an interface may not be supported in C# or many other languages, but it *can* make sense, thus I disagree with answers that state it would generally be pointless. – O. R. Mapper Sep 02 '14 at 11:25
-
@MatíasFidemraizer: I disagree with the claim that the feature would be pointless as a reason for why it is not present in C#. – O. R. Mapper Sep 02 '14 at 13:09
-
@O.R.Mapper At least in SO there's democracy and freedom of mind. Anyway, the *pointless* part have been added by you... I said that we're talking about C# and the question was about C#. This is why maybe my answer *answers* the question. We can discuss about how interfaces might be more useful, but at the end of the day, the tool is C# and based on its limitations (as any language, even regular human languages...) your points have nothing to do with current C# implementation... – Matías Fidemraizer Sep 02 '14 at 13:18
-
@O.R.Mapper For example, I'm Spanish and I would say: why English is n't pronunctiated just as it's written? it would be easier to learn to basically any human in the world if English would be less poluted of arbitrary rules. But if you want to learn English and be an English speaker, and if the question is about English, this language has its rules, strengths and limitations :D – Matías Fidemraizer Sep 02 '14 at 13:20
-
1@MatíasFidemraizer: The question asks why C# doesn't allow any interface member visiblity other than public. When you read all answers and comments on this question, you will see that various users wrote something along the lines of "Interface members in C# can only be public because that is the only way it makes any sense." I have simply explained why I disagree with that claim, by showing how internal interface members could be useful, too. **The question is not asking what is or is not part of the current C# implementation, it is specifically asking for reasons.** – O. R. Mapper Sep 02 '14 at 13:29
-
@O.R.Mapper Reasons behind C# design decisions. I believe that you should add your own answer even if the OP won't see it, because maybe other future readers will find it useful. Why not? – Matías Fidemraizer Sep 02 '14 at 14:24
the Answer is too simple ;
When a class extend from an interface , members and methods o this interface should be accessible by this class , that's why theu have to be public

- 167
- 1
- 1
- 6