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 5981149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:54:12+00:00 2026-05-22T21:54:12+00:00

In C#, we can not change access modifier while overriding a method from base

  • 0

In C#, we can not change access modifier while overriding a method from base class.
e.g.

Class Base
{
   **protected** string foo()
   {
       return "Base";
   }
}

Class Derived : Base
{
   **public** override string foo()
   {
       return "Derived";
   }
}

This is not valid in C#, It will give compile time error.

I want to know the reason, why it’s not allowed. Is there any technical problem or can it lead to something which is not consistent in terms of access restriction???

  • 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-22T21:54:13+00:00Added an answer on May 22, 2026 at 9:54 pm

    Changing the access modifier of a method in a derived type is pointless that’s why it’s not allowed:

    Case 1: Override with a more restrictive access

    This case is obviously not allowed due to the following situation:

    class Base
    {
        public virtual void A() {}
    }
    
    class Derived: Base
    {
        protected override void A()
    }
    

    Now we could say:

    List<Base> list;
    list.Add(new Derived());
    list[0].A() //Runtime access exception
    

    Case 2: Overriding with a less restrictive access modifier

    What is the point? Hide the method and you are done.
    Obviously if someone calls through the base type they will not have access to the new method defined in the derived type but that is consistent with how the author of the base type wanted things to be so you have no “right” to change that. If you want the specifics of the derived class call from the derived class, in which case the new method works perfectly fine.

    EDIT: Expanding case 2

    What I am trying to say in case 2, is that you already have the means to change accessibility of any method (virtual or not) if you want to change accessibility.

    Consider the following code:

    public class Base
    {
        protected virtual string WhoAmI()
        {
            return "Base";
        }
    }
    
    public class Derived : Base
    {
        public new virtual string WhoAmI()
        {
            return "Derived";
        }
    }
    
    public class AnotherDerived : Derived
    {
        public override string WhoAmI()
        {
            return "AnotherDerived";
        }
    }
    

    With the new keyword you have effectively created a new virtual method for your Derived class with the same name and signature. Take note that it is ALLOWED to declare a new method virtual, so any class deriving from Derived will be allowed to override it.

    What is not allowed is to have someone do the following:

     Base newBaseObject = new Derived();
     newBaseObject.WhoAmI() //WhoAmI is not accessible.
    

    But this fact has nothing to do with being able to override WhoAmI() or not. Whatever the case this situation can never be because Base does not declare a public WhoAmI().

    So in a theoretical C# where Derived.WhoAmI() could override Base.WhoAmI() there is no practical benefits in doing so because you will never be able to call the virtual method from the base class anyways, so the new option already meets your requirements.

    I hope this makes it clearer.

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

Sidebar

Related Questions

I am on a shared host and can not change the symbolic link to
This maybe a stupid question, but as I can not easily undo my change
I can not control the scrolling of a scrollpane from within my actionscript (I
The issue is that I can not 'git push' changes from my netbook, to
1) OnCreate is public method of instantiated object from ClsLast class. But I wanted
I am extending a class defined in a library which I cannot change: public
I can not distinguish the difference between aggregating and holding. What does it mean
I just noticed that you can not use standard math operators on an enum
Does GWT has LazyPanel .I can not see it .Please let me know .If
Can I change the code, so that the VS2010 compiler's error message points to

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.