Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5847797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:46:10+00:00 2026-05-22T12:46:10+00:00

Why in C# it is not allowed for derived classes to have greater accessibility

  • 0

Why in C# it is not allowed for derived classes to have greater accessibility than its base class.

For example this will give error : Inconsistent accessibility: base class ‘BaseClass’ is less accessible than class ‘DerivedClass’

internal class BaseClass
{
}

public class DerivedClass : BaseClass
{
}

And why it is allowed in Java.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:46:11+00:00Added an answer on May 22, 2026 at 12:46 pm

    UPDATE: This question was the subject of my blog on November 13th 2012. Thanks for the great question!

    Why in C# it is not allowed for derived classes to have greater accessibility than its base class?

    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:

    public class ComplicatedClass
    {
        public void DoDangerousThing() { ... }
    }
    

    You then write

    public class EvenMoreComplicatedClass : ComplicatedClass
    {
    }
    

    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.

    So Alice changes her code to

    internal class ComplicatedClass
    {
        public void DoDangerousThing() { ... }
    }
    

    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.

    What should happen to your code when Alice recompiles with this change? It should fail to compile. 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.

    Why it is allowed in Java?

    No idea, sorry.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In .net we are not allowed to have shared function/methods in abstract classes and
I'm not sure if this is allowed but I have a main form that
Why is the following C# code not allowed: public abstract class BaseClass { public
Why something like following is not allowed. I mean why the following method will
I have two tables, and they are using different collations. It is not allowed
This should really be allowed - I do not understand why it is not.
Say, the code class Derived: public Base {....} Base* b_ptr = new( malloc(sizeof(Derived)) )
Possible Duplicate: C# Cannot convert from IEnumerable<Base> to IEnumerable<Derived> I have D1 and D2
We have a restriction that a class cannot act as a base-class for more
I have the following class hierarchy in C++: class Base { virtual void apply()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.