开发者

Are private members inherited in C#?

Just seen one tutorial saying that:

Class Dog
{
  private string Name;
}
Class SuperDog:Dog
{
 private string Mood;
}

Then there was an UML displaying that SuperDog will inherit Name as well. I have tried but to me it seems that only public members are inherite开发者_C百科d. At least I could not access Name unless it was declared as public.


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.

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

So, technically, yes, but practically, no.


Everything from the base class is inherited to derived class. members marked private are not accessible to derived classes for integrity purpose, should you need to make them accessible in derived class, mark the members as protected.

There are various levels of members' accessibility in context of inheritance.

public: all public members of the base-class are accessible within the derived-class and to the instances of derived-class.

protected: all protected members of the base-class are accessible within the derived-class and not to the instances of derived-class.

protected internal: all protected internal members of the base-class are accessible within the derived-class and to the instances of derived-class created within the same assembly.

internal: all internal members of the base-class are accessible within the derived-class and to the instances of derived-class within the same assembly.

private: no private members of the base-class are accessible within the derived-class and to the instances of derived-class.

private protected: The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is derived from that class.


SuperDog will inherit the Name field, yes.

SuperDog will NOT have access to the field though, so there is no practical use (as far as SuperDog is concerned).


Private members can be visible inside a derived class: (If the subclass is nested within the base class)

public class Person
{
    private string message;

    public override string ToString()
    {
        return message;
    }

    public static Person CreateEmployee()
    {
        return new Employee();
    }

    class Employee : Person
    {
        public Employee()
        {
            this.message = "I inherit private members!";
        }
    }
}

Credit for the example goes to KodefuGuru in this thread at MSDN.


Yes, although heirs cannot access that member.

If you with that they will be able to access it, declare it as protected.


No, they aren't.

The protected modifier can make fields available to derived classes, but this is generally considered a bad idea from a maintenance perspective. You'd want to use protected properties instead.


try the keyword protected, instead of public/private:

http://msdn.microsoft.com/en-us/library/bcd5672a(VS.71).aspx


Make Name protected or public instead, that will be accessible. Private members are not accessible from derived classes


Private members are not accessible to descendants of a class.

I'm not sure of all the access modifiers, but at the most basic only public and protected members are accessible.


Yes, The are inherited. But you cannot access them as they are private :).


As others have said private members are inherited. Member access is a different subject but not totally disjoint from an inheritance perspective. It is important to understand that all members are inherited regardless of their access modifier because it effects the sizes of the subclasses. Consider the following code.

public class Foo
{
  private int a;
  public int b;
}

public class Bar : Foo
{
  private int c;
  public int d;
}

Foo will consume 16 bytes on the heap. 4 for the syncblock, 4 for the type information (method table), and 4 each for the int variables for a total of 12. Bar, on the other hand, will consume 24 bytes. 4 for the syncblock, 4 for the type information (method table), 4 each for the int fields inherited from Foo, and 4 each for the int fields in Bar for a total of 24.


People have said it, but here's an example of why you need the private fields in the derived class:

class Program
{
    static void Main(string[] args)
    {
        var r = new Random();

        foreach(var i in Enumerable.Range(0,100))            
            new Derived(r).Printer();            

        Console.Read();
    }
}

public class Base
{
    private Random r;
    public Base(Random r) { this.r = r; }

    protected void Print()
    {
        Console.WriteLine(r.Next(1, 10000));
    }
}

public class Derived : Base
{
    public Derived(Random r) : base(r) { }
    public void Printer()
    {
        base.Print();
    }
}


You are right, private members aren't accessible by the derived class.

You should either make the members protected or access them using the public methods of the base class.

class Player
{
    protected string name;
    protected string type;

    public Player(string name,string type)
    {
        Console.WriteLine("Player"+ this);
        this.name = name;
        this.type = type;
    }
   public void introduce()
    {
        Console.WriteLine("I am " + this.name + " I'm a " + this.type);
    }
}

class Wizard : Player
{
    public Wizard(string name,string type):base(name,type)
    {
        Console.WriteLine("Wizard"+ this);
      
    }
    public void Play()
    {
        //protected modifier made the name field available;
        Console.WriteLine("Yipeeee!!!!!"+ " "+ this.name);
        
        //on the other hand
        // we can make the name and type fields as private in the base class and access them using the public methods of the base class
        introduce();

    }


Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited. But yes they really are

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜