67

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Petr
  • 7,787
  • 14
  • 44
  • 53
  • 16
    @Tim Goodman: I assume you mean "if they were *accessible*" and not "if they were *inherited*". – Dirk Vollmar Jun 01 '10 at 15:16
  • The derived class inherits members of its base class(s) whatever the access specifier maybe. The latter affects only who can access them. `struct A{ private: //protected: //public: int x; }; struct B : A{ private: int x; }; int main(){ std::cout << sizeof(A) << '\n'; // 4 on my machine std::cout << sizeof(B) << '\n'; // 8 on my machine std::cout << '\n'; }` – Itachi Uchiwa Nov 01 '21 at 19:17

14 Answers14

117

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Martin Eve
  • 2,703
  • 1
  • 23
  • 23
  • 10
    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. – Aaronaught Jun 01 '10 at 15:22
  • 2
    @Aaronaught: It seems private members can only be accessed using reflection if you walk up the inheritance tree yourself. See http://stackoverflow.com/questions/686482/c-accessing-inherited-private-instance-members-through-reflection/686623#686623 – Dirk Vollmar Jun 01 '10 at 15:38
  • @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. – Aaronaught Jun 01 '10 at 15:44
  • @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. – hemp Jun 01 '10 at 16:05
  • +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! =) – Will Marcouiller Jun 01 '10 at 16:07
  • Inherited, but not accesed. What a bummer! – codegasm Aug 23 '14 at 14:55
54

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.

Aslam Jiffry
  • 1,306
  • 6
  • 23
  • 57
this. __curious_geek
  • 42,787
  • 22
  • 113
  • 137
  • 1
    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. – Anthony Pegram Jun 02 '10 at 13:08
20

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

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
2

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.

user781310
  • 83
  • 1
  • 1
  • 7
Jørgen Fogh
  • 7,516
  • 2
  • 36
  • 46
1

Yes, although heirs cannot access that member.

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

Poni
  • 11,061
  • 25
  • 80
  • 121
0

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();
    }
}
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
0

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.

Warren Rumak
  • 3,824
  • 22
  • 30
  • 7
    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?? – marc_s Jun 01 '10 at 15:01
  • 4
    -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. – Niki Jun 01 '10 at 15:07
  • 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. – Warren Rumak Jun 01 '10 at 15:10
  • 2
    @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*. – Niki Jun 01 '10 at 15:17
  • 1
    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. – Warren Rumak Jun 01 '10 at 16:03
  • 1
    @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. – Niki Jun 01 '10 at 16:57
0

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.

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
0

try the keyword protected, instead of public/private:

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

dm76
  • 4,130
  • 8
  • 35
  • 46
0

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

Greg Olmstead
  • 1,551
  • 10
  • 22
0

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.

Brian Gideon
  • 47,849
  • 13
  • 107
  • 150
0

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

Ram
  • 11,404
  • 15
  • 62
  • 93
0

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

    }
Cleptus
  • 3,446
  • 4
  • 28
  • 34
-1

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

Spooks
  • 6,937
  • 11
  • 49
  • 66