<p>In C#, when you implement an interface, all members are implicitly public. Wouldn't it be better if we could specify the accessibility modifier (<code>protected</code>, <code>internal</code>, except <code>private</code> of course), or should we just use an abstract class instead?</p>

[{'answer_id': 17577, 'author': 'Ishmaeel', 'author_id': 227, 'author_profile': 'https://Stackoverflow.com/users/227', 'pm_score': 3, 'selected': False, 'text': '<p>Would not make sense. An Interface is a contract with the <strong>public</strong> that you support those methods and properties. Stick with abstract classes.</p>\n'}, {'answer_id': 17579, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 1, 'selected': False, 'text': '<p>Interfaces do not have access modifiers in their methods, leaving them open to whichever access modifier is appropriate. This has a purpose: it allows other types to infer what methods and properties are available for an object following an interface. Giving them protected/internal accessors defeats the purpose of an interface.</p>\n\n<p>If you are adamant that you need to provide an access modifier for a method, either leave it out of the interface, or as you said, use an abstract class.</p>\n'}, {'answer_id': 17580, 'author': 'John Topley', 'author_id': 1450, 'author_profile': 'https://Stackoverflow.com/users/1450', 'pm_score': 0, 'selected': False, 'text': "<p>I'm familiar with Java rather than C#, but why an earth would you want a private member within an interface? It couldn't have any implementation and would be invisible to implementing classes, so would be useless. Interfaces exist to specify behaviour. If you need default behaviour than use an abstract class.</p>\n"}, {'answer_id': 17591, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 5, 'selected': False, 'text': "<p>If an interface is internal, all its members will be internal to the assembly. If a nested interface is protected, only the subclasses of the outer class could access that interface.</p>\n\n<p>Internal members for an interface outside of its declaring assembly would be pointless, as would protected members for an interface outside of its declaring outer class.</p>\n\n<p>The point of an interface is to describe a <strong>contract</strong> between a implementing type and users of the interface. Outside callers aren't going to care  and shouldn't <strong>have</strong> to care about implementation, which is what internal and protected members are for.</p>\n\n<p>For protected members that are called by a base class, abstract classes are the way to go for specifying a contract between base classes and classes that inherit from them. But in this case, implementation details are usually very relevant, unless it's a degenerate pure abstract class (where <em>all</em> members are abstract) in which case protected members are useless. In that case, go with an interface and save the single base class for implementing types to choose.</p>\n"}, {'answer_id': 17598, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 4, 'selected': False, 'text': '<p>You can hide the implementation of an interface by explicitly stating the interface name before the method name:</p>\n\n<pre><code>public interface IInterface {\n    public void Method();\n}\n\npublic class A : IInterface {\n    public void IInterface.Method() {\n        // Do something\n    }\n}\n\npublic class Program {\n    public static void Main() {\n        A o = new A();\n        o.Method(); // Will not compile\n        ((IInterface)o).Method(); // Will compile\n    }\n}\n</code></pre>\n'}, {'answer_id': 17610, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 2, 'selected': False, 'text': '<p>An interface is a contract that all implementing classes adhere to. This means that they must adhere to all of it or none of it.</p>\n\n<p>If the interface is public then every part of that contact has to be public, otherwise it would mean one to friend/internal classes and a different thing to everything else.</p>\n\n<p>Either use an abstract base class or (if possible and practical) an <a href="https://stackoverflow.com/questions/8042/extension-interface-patterns">internal extension method on the interface</a>.</p>\n'}, {'answer_id': 251340, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>You can hide almost all of the code implemented by interfaces to external assemblies.</p>\n\n<pre><code>interface IVehicle\n{\n    void Drive();\n    void Steer();\n    void UseHook();\n}\nabstract class Vehicle  // :IVehicle  // Try it and see!\n{\n    /// &lt;summary&gt;\n    /// Consuming classes are not required to implement this method.\n    /// &lt;/summary&gt;\n    protected virtual void Hook()\n    {\n        return;\n    }\n}\nclass Car : Vehicle, IVehicle\n{\n    protected override void Hook()  // you must use keyword "override"\n    {\n        Console.WriteLine(" Car.Hook(): Uses abstracted method.");\n    }\n    #region IVehicle Members\n\n    public void Drive()\n    {\n        Console.WriteLine(" Car.Drive(): Uses a tires and a motor.");\n    }\n\n    public void Steer()\n    {\n        Console.WriteLine(" Car.Steer(): Uses a steering wheel.");\n    }\n    /// &lt;summary&gt;\n    /// This code is duplicated in implementing classes.  Hmm.\n    /// &lt;/summary&gt;\n    void IVehicle.UseHook()\n    {\n        this.Hook();\n    }\n\n    #endregion\n}\nclass Airplane : Vehicle, IVehicle\n{\n    protected override void Hook()  // you must use keyword "override"\n    {\n        Console.WriteLine(" Airplane.Hook(): Uses abstracted method.");\n    }\n    #region IVehicle Members\n\n    public void Drive()\n    {\n        Console.WriteLine(" Airplane.Drive(): Uses wings and a motor.");\n    }\n\n    public void Steer()\n    {\n        Console.WriteLine(" Airplane.Steer(): Uses a control stick.");\n    }\n    /// &lt;summary&gt;\n    /// This code is duplicated in implementing classes.  Hmm.\n    /// &lt;/summary&gt;\n    void IVehicle.UseHook()\n    {\n        this.Hook();\n    }\n\n    #endregion\n}\n</code></pre>\n\n<p>This will test the code.</p>\n\n<pre><code>class Program\n{\n    static void Main(string[] args)\n    {\n        Car car = new Car();\n        IVehicle contract = (IVehicle)car;\n        UseContract(contract);  // This line is identical...\n        Airplane airplane = new Airplane();\n        contract = (IVehicle)airplane;\n        UseContract(contract);  // ...to the line above!\n    }\n\n    private static void UseContract(IVehicle contract)\n    {\n        // Try typing these 3 lines yourself, watch IDE behavior.\n        contract.Drive();\n        contract.Steer();\n        contract.UseHook();\n        Console.WriteLine("Press any key to continue...");\n        Console.ReadLine();\n    }\n}\n</code></pre>\n'}, {'answer_id': 12572125, 'author': 'nawfal', 'author_id': 661933, 'author_profile': 'https://Stackoverflow.com/users/661933', 'pm_score': 3, 'selected': False, 'text': '<p>All the answers here more or less say <strong>that\'s how interfaces are meant to be, they are universal public specifications.</strong></p>\n\n<p>This being the most discussed thread, let me post two excellent answers I found on SO when this question surfaced my mind.</p>\n\n<p><a href="https://stackoverflow.com/a/7238707/661933">This answer gives an example</a> of how it can be nonsensical to have non uniform access specifiers for interface members in derived classes. Code always better than technical descriptions. </p>\n\n<p>To me the most damning thing about forced public interface members are that the interface itself can be internal to an assembly but the members it exposes have to be public. <a href="https://stackoverflow.com/a/11162397/661933">Jon Skeet explains here that\'s by design sadly</a>.</p>\n\n<p>That raises the question why weren\'t interfaces designed to have non-public definitions for members. That can make the contract flexible. This is pretty useful when writing assemblies where you dont want specific members of classes to be exposed to outside the assembly. I do not know why.</p>\n'}, {'answer_id': 42755575, 'author': 'Ini', 'author_id': 4764917, 'author_profile': 'https://Stackoverflow.com/users/4764917', 'pm_score': 0, 'selected': False, 'text': '<p>In my opintion this violates encapsulation. I have to implement a methos as public then I implement an interface. I see no reason to force public in a class that impletements the interface. (c#)</p>\n'}]
['https://Stackoverflow.com/questions/17576', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/718/']