Are private members inherited in C#?

C#InheritancePrivate

C# Problem Overview


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 inherited. At least I could not access Name unless it was declared as public.

C# Solutions


Solution 1 - C#

> 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.

Solution 2 - C#

> 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.

Solution 3 - C#

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).

Solution 4 - C#

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.

Solution 5 - C#

Yes, although heirs cannot access that member.

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

Solution 6 - C#

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.

Solution 7 - C#

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.

Solution 8 - C#

try the keyword protected, instead of public/private:

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

Solution 9 - C#

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

Solution 10 - C#

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.

Solution 11 - C#

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

Solution 12 - C#

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();
    }
}

Solution 13 - C#

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();

    }

Solution 14 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPetrView Question on Stackoverflow
Solution 1 - C#Martin EveView Answer on Stackoverflow
Solution 2 - C#this. __curious_geekView Answer on Stackoverflow
Solution 3 - C#Justin NiessnerView Answer on Stackoverflow
Solution 4 - C#Jørgen FoghView Answer on Stackoverflow
Solution 5 - C#PoniView Answer on Stackoverflow
Solution 6 - C#Warren RumakView Answer on Stackoverflow
Solution 7 - C#Matt EllenView Answer on Stackoverflow
Solution 8 - C#dm76View Answer on Stackoverflow
Solution 9 - C#Greg OlmsteadView Answer on Stackoverflow
Solution 10 - C#Brian GideonView Answer on Stackoverflow
Solution 11 - C#RamView Answer on Stackoverflow
Solution 12 - C#JonesopolisView Answer on Stackoverflow
Solution 13 - C#sruthivasudevanView Answer on Stackoverflow
Solution 14 - C#SpooksView Answer on Stackoverflow