Are private members inherited in C#?

80,974

Solution 1

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

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

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

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

Yes, although heirs cannot access that member.

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

Share:
80,974
Petr
Author by

Petr

Updated on September 21, 2021

Comments

  • Petr
    Petr over 2 years

    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.

  • Cylon Cat
    Cylon Cat almost 14 years
    All access modifiers other than private are inherited.
  • marc_s
    marc_s almost 14 years
    sure??? I would believe they are inherited - just not accessible to the descendant class. Why? What happens if you cast your descendant to the base class - if the base class' private members aren't inherited.... then what??
  • Anthony Pegram
    Anthony Pegram almost 14 years
    and @Cylon, see the top answer. Inherited != Accessible.
  • Niki
    Niki almost 14 years
    -1 They are inherited. They just aren't visible to the derived class. The question seems to come from someone new to OOP, don't confuse them by mixing up terminology.
  • Warren Rumak
    Warren Rumak almost 14 years
    Marc, you can't use casting to access private field data. Privates can only be accessed using an explicit or implicit this modifier, which means the code has to be running inside the class. Furthermore, if you cast this to another class (even its own superclass), you would only be able to access public items, not protected or private ones.
  • Niki
    Niki almost 14 years
    @Warren: Read the question again. He knows he can't access private members of a base class. He wants to know if they are there.
  • Aaronaught
    Aaronaught almost 14 years
    I always use the documentation as the actual "contract", but as a point of interest, the .NET Reflection API doesn't seem to agree on this point. If you try to pull the private field from the derived class using derivedType.GetField("f", BindingFlags.Instance | BindingFlags.NonPublic), you will get nothing back, even though this is technically supposed to return all inherited members. So even though it technically must exist, under the hood, all relevant parts of the runtime treat it as though it does not actually exist.
  • Dirk Vollmar
    Dirk Vollmar almost 14 years
    @Aaronaught: It seems private members can only be accessed using reflection if you walk up the inheritance tree yourself. See stackoverflow.com/questions/686482/…
  • Aaronaught
    Aaronaught almost 14 years
    @0xA3: Yes, I'm aware of how to get around that issue, I was just pointing out that the API doesn't seem to tell the same story that the documentation does. As far as the API is concerned, private members are not inherited.
  • Warren Rumak
    Warren Rumak almost 14 years
    Well, are they? It's an interesting question, because the documentation says it's there, but there's no actual technical proof of it! Consider: class X has private member A; class Y derives from class X. typeof(Y).GetField("A", Instance | NonPublic | FlattenHierarchy) returns null.... typeof(Y).BaseType.GetField("A", Instance | NonPublic | FlattenHierarchy) returns a FieldInfo for A. So, again, -no-, the fields aren't inherited as far as the .NET Framework is concerned. They're still -solely- in the base class.
  • hemp
    hemp almost 14 years
    @Aaronaught: That'd be a "design decision", known to virtually everyone outside of MS as a "bug". Excellent point, though, and very relevant to the posted answer.
  • Will Marcouiller
    Will Marcouiller almost 14 years
    +1 At first sight, I would have said: "No, they're private!" Nice answer though, because on second thought, behaviour given through private fields using methods or whatsoever, is inherited. This means that the derived type only doesn't have access to them. Very slight difference though! Great! =)
  • Niki
    Niki almost 14 years
    @Warren: The "proof" is simple: Write a base class with a private member. Make a protected or public method that uses the private member. Call that method on an instance of a derived class. It can access the private field although there is no instance of the base class around.
  • Anthony Pegram
    Anthony Pegram almost 14 years
    I think your wording on protected internal is off. protected internal base class members are visible to all derived classes and are exposed to all classes within the same assembly as the base.
  • codegasm
    codegasm over 9 years
    Inherited, but not accesed. What a bummer!
  • Srikanth
    Srikanth almost 5 years
    not a concrete statement, Put your point strait , looks more of diplomatic answer