0

If we inherit a class do the private variables also get inherited?

I know that "Yes, the variables are inherited but cannot be accessed directly by the class interface."

What I want to know is how can we access the private variables/methods from the child class

What I am trying to say is that private members are also inherited.But how to access the same without making them protected.

5 Answers5

10

I know that "Yes, the variables are inherited but cannot be accessed directly by the class interface."

So you know the answer then.

What I want to know is how can we access the private variables/methods from the child class

You can’t, that’s why they are private (rather than, say, protected). The whole intention of making them private is so that you cannot access them from anywhere, notably including child classes.

Explicitly breaking this encapsulation is almost always a sign of a broken design and shouldn’t ever be part of a normal code flow. However, there are situations in which you want to reason about some code, and in these situations it may be necessary to examine even private values. Reflection libraries allow this. Here’s a simple example using the System.Reflection capabilities:

class Widget {
    private readonly string identifier;

    public Widget(string identifier) {
        this.identifier = identifier;
    }
}

class MainClass {
    public static void Main(string[] args) {
        var widget = new Widget("my_test_widget");

        var type = widget.GetType();
        var field = type.GetField("identifier",
                                  System.Reflection.BindingFlags.Instance |
                                  System.Reflection.BindingFlags.NonPublic);

        Console.WriteLine($"{field} = {field.GetValue(widget)}");
    }
}
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
5

Make them protected. For variables, make a protected property that the child classes then use.

Steve
  • 2,205
  • 1
  • 21
  • 28
  • 1
    +1 for protected properties. Protected contract is a contract too... It's a bad idea - to expose protected fields. – Dennis May 29 '12 at 12:07
  • From the question "But how to access the same without making them protected." – Matsemann May 29 '12 at 12:43
  • @Matsemann The question was edited after I answered. That constraint wasn't there when I answered. – Steve May 29 '12 at 12:54
4

You can only access private variables/methods from a derived class using reflection. You cannot access them in a "natural" way, since the whole point of making them private is to hide them from other classes (including derived classes).

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
2

Make it protected property instead of private member.

Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
1

What I want to know is how can we access the private variables/methods from the child class ......... But how to access the same without making them protected.

You might want to try using reflection:
Here is a similar question / answer that explains how this can be done.

Community
  • 1
  • 1
Blachshma
  • 17,097
  • 4
  • 58
  • 72