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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:53:44+00:00 2026-06-05T09:53:44+00:00

I can’t wrap my head around why m() in class a can access x

  • 0

I can’t wrap my head around why m() in class a can access x and y through the b class and b class object if x and y are private. I know that when b inherits from a, b receives private members from a even though they can’t be used by b. But what is strange to me is that b members can’t use x and y, and classes other than a can’t get at the variables through b class and b class object, yet m() can access x and y through the b class and b class object.

Can someone explain this to me using a general rule that I missed or maybe an explanation about how the compiler does this ‘giving’ of base members to derived classes?

class a
{
    private int x;
    private static int y;

    static void m()
    {
        b bobj = new b();
        int mm = bobj.x;
        int rr = b.y;


    }

    void n()
    {
        b bobj = new b();
        int mm = bobj.x;
        int rr = b.y;
    }
}

class b : a
{
    private int u;
    private static int v;

    static void o()
    {

    }

    void p()
    {

    }
}
  • 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-05T09:53:47+00:00Added an answer on June 5, 2026 at 9:53 am

    I can’t wrap my head around why m() in class a can access x and y through the b class and b class object if x and y are private

    Code within a class declaration can access any private members declared by that class – it’s as simple as that. So code within a can’t access private variables declared in b, but it can access private variables declared in a via an instance of a which also happens to be an instance of b.

    Note that this line:

    int rr = b.y;
    

    is effectively converted to

    int rr = a.y;
    

    y is only declared by a – if it were really declared by b, it wouldn’t be accessible.

    See section 3.5 of the C# 4 language specification for more details.

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

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
Can you cast a List<int> to List<string> somehow? I know I could loop through
Can we change the color of the divider? Apple documentations says, that we can
Can someone please explain why this doesn't work? MyClass myClass1 = new MyClass(); object
Can anybody tell me that what is suitable Xcode version for 10.6.8(snow leopard)?Does Xcode
Can I register a .Net COM class with the SingleUse -flag? The reason I
Can MOSS integrate and get user profiles from multiple Active Directory and/or LDAP stores?
Can a Visual Studio 2008 custom tool be passed additional parameters from the custom
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
can anyone tell me if it is possible to convert a dojo charting object

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.