Advertisement
Advanced C# Programming, Access Modifiers in C#

Can derived classes have greater accessibility than their base types?

Short answer

No, a derived class cannot have greater accessibility than its base class, since doing so would create a situation where code could access the derived class but not the base class it depends on, which would be inconsistent and is disallowed by the compiler.

Save C# Programming mock interview
Advertisement
AModel answer
No, a derived class cannot have greater accessibility than its base class, since doing so would create a situation where code could access the derived class but not the base class it depends on, which would be inconsistent and is disallowed by the compiler. For example, a public class cannot inherit from an internal base class if that public class is meant to be used outside the current assembly, since external code would not be able to see the base type it depends on.
// This causes a compiler error if Base is internal and Derived is public
internal class Base { }
// public class Derived : Base { } // not allowed
Real-world example
A library author keeps a helper base class internal while trying to expose a derived class as public, and encounters a compiler error explaining the accessibility mismatch, prompting them to make the base class public as well.
Likely follow-ups
  • What compiler error is shown when this rule is violated?
  • Does this rule apply the same way to interfaces as it does to classes?
Related topics