简体   繁体   中英

inheritance of private members in c#

Are private members inherited when inheriting the class in c#? I have read some topic related to this, somebody telling that private members are inherited but cannot access the private members, somebody telling that it is not inherited when inheriting the class. Please explain the concept. if it is inheriting can any body give an explanation?

thanks

If I understand your question correctly then you're not concerned about the accessibility you are only concerned about private members are inherited or not

Answer is yes, all private members are inherited but you cant access them without reflection.

public class Base
{
    private int value = 5;

    public int GetValue()
    {
        return value;
    }
}

public class Inherited : Base
{
    public void PrintValue()
    {
        Console.WriteLine(GetValue());
    }
}

static void Main()
{
    new Inherited().PrintValue();//prints 5
}

You can mark things as protected , in which case you can access them from derived types.

Edit: In terms of whether they inherit, then yes they do. The child class is still of the parent classes type, and thus inherits everything. The child just cannot access it directly, but if you call base class methods that use the private parent field, that would work fine.

Like Sriram says, yes, private members do get inherited, but they ar enot accessible.

If they would not get inherited, protected or public properties references private members would break in inherited classes.

class myBase
{
  private string _myProp;
  protected string MyProp
  { 
    get 
    {
      return _myProp;
    }
    set
    {
      _myProp = value;
    }
  }
}
class myChild : myBase
{
  public myChild()
  {
    _myProp = "SomeString"; // This will fail!!!
    this.Myprop = "SomeString"; // This works
  }
}

Here in the child class, you cannot access _myProp directly as it is private in the base class. However, the memeber is inherited, so it is accessible through the protected property MyProp.

Members marked as private can be accessed only on the type where they are defined. You cannot access them from derived types.

Members marked as protected can be accessed on the type where they are defined and on derived types.

Members marked as internal can be accessed only from the assembly where the type is defined. You may combine the protected and the internal access modifier.

When talking about values of private members: Of course they are inherited. A derived class always also is of the type of the base class. If the base class holds a private value to store some data, the derived class will do that, too - only that you can't access that value from the derived class.

Please also read the relevant article on Accessibility Levels in the MSDN .

From http://msdn.microsoft.com/en-us/library/ms173149.aspx

A derived class has access to the public, protected, internal, and protected internal members of a base class. Even though a derived class inherits the private members of a base class, it cannot access those members. However, all those private members are still present in the derived class and can do the same work they would do in the base class itself. For example, suppose that a protected base class method accesses a private field. That field has to be present in the derived class in order for the inherited base class method to work properly.

私有成员不能被继承,只能被保护,就像扩展的私有成员一样。

What you said about private fields being inherited is totally right.

Here what happens: A subclass will inherit the behaviour of your base class. That behaviour might need some fields to work. So, your subclass will reserve some space for them, but you will not be able to manipulate those fields, only by using methods (public and protected ones)

In other words, your sub class inherits base class fields by holding them in memory, but it cannot access them.

On low level, it is your compiler that prevents you from accessing/changing those private fields, but even using reflection you can still do so.

If you need any clarification, let me know

Short answer: yes, but you are unable to access them directly.

When class A is derived from class B, and B has a private property then that property will be there for an instance derived of class A. You are, however unable to access it. It must be there because the behavour specified in class B depends on it.

funfact: It is actually possible to still access the private property with a thing called reflection. But don't worry about that and you should not use reflection for this purpose unless you really need to know what is going on and what you're doing.

To expand on what's been said, privates can be accessed by a subclass when inner scope is in play. For example, the following will compile:

class A
{
    private int _private;

    class B : A
    {
        void Foo()
        {
            this._private = 2;
        }
    }
}

The simplest answer you can not access the private variables of base class in derived class directly but yes this private objects and values are initialized in base class when overriding class object is initiated (thus base class variables are inherited) and you can access them using some property or function if base class exposes them.
To make explanation more clear,

    class A 
    {
        private int i;
        private int j;
        protected int k;

        public A()
        {
            i = j = k = 5;
        }
    }


    class B : A
    {
        private int i; //The same variable exist in base class but since it is private I can declare it
        private int j;
        private int k; //Here I get warning, B.k hides inherited member A.k'. Use the new keyword if hiding was intended.   F:\Deepak\deepak\Learning\ClientUdpSocketCommunication\ClientUdpSocketCommunication\Program.cs  210 25  ClientUdpSocketCommunication

        private int l;
        private int m;
        private int n;

        public B()
        {
            i= j = this.k = l = m = n = 7; // Here I have used this.k to tell compiler that I want to initialize value of k variable of B.k class
            base.k = 5; //I am assigning and accessing base class variable as it is protected

        }
    }

If an object of class B is initialized then, Ai, Aj, Ak variable will be initialized, with Bi, Bj, Bk, Bl variables and if base class exposes function or properties then I can access all the base class variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM