Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>UPDATE</strong>: This question was <a href="http://blogs.msdn.com/b/ericlippert/archive/2012/11/13/why-is-deriving-a-public-class-from-an-internal-class-illegal.aspx" rel="nofollow">the subject of my blog on November 13th 2012</a>. Thanks for the great question!</p> <blockquote> <p>Why in C# it is not allowed for derived classes to have greater accessibility than its base class?</p> </blockquote> <p>In addition to the other good answers, consider this scenario. You and your coworker Alice are working on different parts of the same assembly. Alice writes a class:</p> <pre><code>public class ComplicatedClass { public void DoDangerousThing() { ... } } </code></pre> <p>You then write</p> <pre><code>public class EvenMoreComplicatedClass : ComplicatedClass { } </code></pre> <p>Great. Now Alice gets a security review from Bob, and they realize that (1) no customer ever needs to use ComplicatedClass, and (2) DoDangerousThing exposes a security hole that hostile code could take advantage of. Internal code of course does not.</p> <p>So Alice changes her code to</p> <pre><code>internal class ComplicatedClass { public void DoDangerousThing() { ... } } </code></pre> <p>Figuring there is no need to change "public" to "internal" on the method because of course public means "public to things that can see this class", and now no external code can see this class.</p> <p>What should happen to your code when Alice recompiles with this change? <strong>It should fail to compile</strong>. You have made an assumption that users need to see the base class, and Alice has made a change that violates that assumption. The safe thing to do is for the compiler to tell Alice that she needs to come talk to you so that you can resolve this problem without exposing the customer to the vulnerability of DoDangerousThing.</p> <blockquote> <p>Why it is allowed in Java?</p> </blockquote> <p>No idea, sorry.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload