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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:49:26+00:00 2026-06-02T03:49:26+00:00

I do not think this is possible to do, but I am curious. Is

  • 0

I do not think this is possible to do, but I am curious. Is it possible to make a protected variable in a super class private in its subclass.

For example, I have three classes SuperClass, SubClass : SuperClass and SubCLass2 : SubClass.

I want to be able to access variable protected string[] commands that is in SuperClass from SubClass but prevent access to it from SubClass2.

If this is not possible, is there anyway to achieve the same effect?

  • 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-06-02T03:49:32+00:00Added an answer on June 2, 2026 at 3:49 am

    I do not think this is possible to do, but I am curious. Is it possible to make a protected variable in a super class private in its subclass.

    No. Personally, I would avoid having the variable non-private in the first place – I’d provide a protected property. You could create a new property to “hide” the base property… but this smacks of poor design to me.

    I can’t think of any way of getting around the hiding from the grand-child’s point of view, without changing one of the other classes, but it feels pretty arbitrary to me:

    public class Base
    {
        private int foo = 5;
        protected int Foo { get { return foo; } }
    }
    
    public class Child : Base
    {
        protected new int Foo { get { return 0; } }
    }
    
    public class GrandChild : Child
    {
        // Aargh, can't get at the original Foo...
    }
    

    Why do you want to achieve this? Perhaps you should seal your direct subclass, and force other classes to use composition instead…

    EDIT: As explained in the comments, it sounds like this is a situation where making the property (and I would still make it a property returning a private field) internal would be appropriate. That gives access to the property to all other code in the same assembly, but no other assemblies.

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

Sidebar

Related Questions

I do not think this is possible and curious if it is or why
so I'm not entirely sure this is possible, but I am curious if it
I think this is a simple question, but I can not find the answer
This is not 100% programming related. But I think this is somewhat useful because
I'm not sure how to describe this problem, so I think an example is
I'm curious whether this is possible, or as I suspect, by design not. In
I am not familiar with Ivy. I think this is some utility to automatically
I will preface this question by saying, I do not think it is solvable.
I've been searching for a while and everybody seems to think this is not
Sorry for the slightly rubbish title. I could not think how to describe this

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.