<p>Does C# have the notion of private / protected inheritance, and if not, why?</p>

<p><strong>C++</strong></p>

<pre>
<code>
class Foo : private Bar {
 public:
   ...
 }; 
</code>
</pre>

<p><strong>C#</strong></p>

<pre>
<code>
public abstract NServlet class : private System.Web.UI.Page
{
    // error "type expected"
}
</code>
</pre>

<p>I am implementing a "servlet like" concept in an .aspx page and I don't want the concrete class to have the ability to see the internals of the System.Web.UI.Page base.</p>

[{'answer_id': 33128, 'author': 'Chris Karcher', 'author_id': 2773, 'author_profile': 'https://Stackoverflow.com/users/2773', 'pm_score': 1, 'selected': False, 'text': '<p>No, public inheritance only.</p>\n'}, {'answer_id': 33142, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 1, 'selected': False, 'text': '<p>You probably want a ServletContainer class that gets hooked up with a NServlet implementation. In my book, not allowing private / protected inheritance is not really a big deal and keeps the language less confusing - with LINQ etc. we allready have enough stuff to remember.</p>\n'}, {'answer_id': 33151, 'author': 'Scott Dorman', 'author_id': 1559, 'author_profile': 'https://Stackoverflow.com/users/1559', 'pm_score': -1, 'selected': False, 'text': "<p>No it doesn't. What would the benefit be of allowing this type of restriction?</p>\n"}, {'answer_id': 33152, 'author': 'bdukes', 'author_id': 2688, 'author_profile': 'https://Stackoverflow.com/users/2688', 'pm_score': 3, 'selected': False, 'text': '<p>You can hide inherited APIs from being publicly visible by declaring that same member in your class as private, and using the new keyword.  See <a href="http://msdn.microsoft.com/en-us/library/aa691135.aspx" rel="noreferrer">Hiding through Inheritance</a> from MSDN.</p>\n'}, {'answer_id': 33155, 'author': 'Joel Martinez', 'author_id': 3433, 'author_profile': 'https://Stackoverflow.com/users/3433', 'pm_score': 2, 'selected': False, 'text': "<p>If you want the NServlet class to not know anything about the Page, you should look into using the Adapter pattern.  Write a page that will host an instance of the NServlet class.  Depending on what exactly you're doing, you could then write a wide array of classes that only know about the base class NServlet without having to pollute your API with asp.net page members.</p>\n"}, {'answer_id': 33180, 'author': 'Brian Stewart', 'author_id': 3114, 'author_profile': 'https://Stackoverflow.com/users/3114', 'pm_score': 4, 'selected': False, 'text': '<p>C# allows public inheritance only. C++ allowed all three kinds. Public inheritance implied an "IS-A" type of relationship, and private inheritance implied a "Is-Implemented-In-Terms-Of" kind of relationship. Since layering (or composition) accomplished this in an arguably simpler fashion, private inheritance was only used when absolutely required by protected members or virtual functions required it - according to Scott Meyers in Effective C++, Item 42. </p>\n\n<p>My guess would be that the authors of C# did not feel this additional method of implementing one class in terms of another was necessary.</p>\n'}, {'answer_id': 33182, 'author': 'Chris Karcher', 'author_id': 2773, 'author_profile': 'https://Stackoverflow.com/users/2773', 'pm_score': 2, 'selected': False, 'text': "<p>@bdukes:\nKeep in mind that you aren't <strong>truly</strong> hiding the member. E.g.:</p>\n\n<pre><code>class Base\n{\n   public void F() {}\n}\nclass Derived : Base\n{\n   new private void F() {}\n}\n\nBase o = new Derived();\no.F();  // works\n</code></pre>\n\n<p>But this accomplishes the same as private inheritance in C++, which is what the questioner wanted.</p>\n"}, {'answer_id': 2321706, 'author': 'Nick Alexeev', 'author_id': 279844, 'author_profile': 'https://Stackoverflow.com/users/279844', 'pm_score': 3, 'selected': False, 'text': '<blockquote>\n  <p>No it doesn\'t. What would the benefit be of allowing this type of restriction?</p>\n</blockquote>\n\n<p>Private and protected inheritance is good for encapsulation (information hiding).  Protected* inheritance is supported in C++, although it isn’t in Java.  Here’s an example from my project where it would be useful.</p>\n\n<p>There is a base class in as 3rd party framework**.  It has dozens of settings plus properties and methods for manipulating them.  The base class doesn’t make a lot of checking when individual settings are assigned, but it will generate an exception later if it encounters an unacceptable combination.</p>\n\n<p>I’m making a child class with methods for assigning these settings (e.g. example, assigning carefully crafted settings from a file).  It would be nice to deny the rest of the code (outside my child class) the ability to manipulate individual settings and mess them up.</p>\n\n<p>That said, I think in C++ (which, again, supports private and protected inheritance) it\'s possible to cast the child class up to parent and get access to parent\'s public members.  (See also <a href="https://stackoverflow.com/questions/33115/does-c-have-the-notion-of-private-and-protected-inheritance/33182#33182">Chris Karcher\'s post</a>)  Still, protected inheritance improves information hiding.  If members of a class B1 need to be truly hidden within other classes C1 and C2, it can be arranged by making a protected variable of a class B1 within C1 and C2.  Protected instance of B1 will be available to children of C1 and C2.  Of course, this approach by itself doesn\'t provide polymorphism between C1 and C2.  But polymorphism can be added (if desired) by inheriting C1 and C2 from a common interface I1.</p>\n\n<p>*** For brevity will use "protected" instead of "private and protected".</p>\n\n<p>** National Instruments Measurement Studio in my case.</p>\n\n<ul>\n<li>Nick</li>\n</ul>\n'}, {'answer_id': 11171448, 'author': 'ulatekh', 'author_id': 603828, 'author_profile': 'https://Stackoverflow.com/users/603828', 'pm_score': 1, 'selected': False, 'text': '<p>I know this is an old question, but I\'ve run into this issue several times while writing C#, and I want to know...why not just use an interface?</p>\n\n<p>When you create your subclass of the 3rd party framework\'s class, also have it implement a public interface.  Then define that interface to include only the methods that you want the client to access.  Then, when the client requests an instance of that class, give them an instance of that interface instead.</p>\n\n<p>That seems to be the C#-accepted way of doing these sorts of things.</p>\n\n<p>The first time I did this was when I realized that the C# standard library didn\'t have a read-only variant of a dictionary.  I wanted to provide access to a dictionary, but didn\'t want to give the client the ability to change items in the dictionary.  So I defined a "class DictionaryEx&lt;K,V,IV&gt; : Dictionary&lt;K,V&gt;, IReadOnlyDictionary&lt;K,IV&gt; where V : IV" where K is the key type, V is the real value type, and IV is an interface to the V type that prevents changes.  The implementation of DictionaryEx was mostly straightforward; the only difficult part was creating a ReadOnlyEnumerator class, but even that didn\'t take very long.</p>\n\n<p>The only drawback I can see to this approach is if the client tries to dynamically cast your public interface to the related subclass.  To stop this, make your class internal.  If your client casts your public interface to the original base class, I think it\'d be pretty clear to them that they\'re taking their life in their own hands. :-)</p>\n'}, {'answer_id': 32614798, 'author': 'franckspike', 'author_id': 1368184, 'author_profile': 'https://Stackoverflow.com/users/1368184', 'pm_score': 1, 'selected': False, 'text': '<p><strong>First solution:</strong></p>\n\n<p><strong>protected internal</strong> acts as public in the same assembly and protected on other assemblies.</p>\n\n<p>You would need to change the access modifier of each members of the class which are not to be exposed through inheritance.</p>\n\n<p>It is a bit restrictive though that this solution <strong>requires and forces the class to be inherited</strong> to be used by another assembly. Thus the choice of being used only by inheritance or not is taken by the unknowing parent... normally the children are more knowing of the architecture...</p>\n\n<p>Not a perfect solution but might be a better <strong>alternative to adding an interface to hide methods</strong> and still leaving the possibility of using the parent methods to be hidden though the child class because you might not easily be able to force the use of the interface.</p>\n\n<hr>\n\n<p><strong>Problem:</strong></p>\n\n<p>The <strong>protected</strong> and <strong>private</strong> access modifiers <strong>cannot</strong> be used for methods that are <strong>implement</strong>ing <strong>interfaces</strong>. That means that the protected internal solution cannot be used for interface implemented methods. This is a big restriction.</p>\n\n<hr>\n\n<p><strong>Final solution:</strong></p>\n\n<p>I fell back to the <strong>interface</strong> solution <strong>to hide methods</strong>.</p>\n\n<p>The problem with it was to be able to force the use of the interface so that members to be hidden are ALWAYS hidden and then definitely avoiding mistakes.</p>\n\n<p>To <strong>force using only the interface</strong>, just <strong>make the constructors protected</strong> and <strong>add a static method for construction</strong> (I named it New). This static New method is in fact a factory function and it <strong>returns the interface</strong>. So the rest of the code has to use the interface only!</p>\n'}]
['https://Stackoverflow.com/questions/33115', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1638/']