views:

625

answers:

12

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.

A: 

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
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
-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.
nikie
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
@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*.
nikie
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
@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.
nikie
A: 

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
All access modifiers other than private are inherited.
Cylon Cat
@Matt and @Cylon, see the top answer. Inherited != Accessible.
Anthony Pegram
Thanks anthony. Hadn't noticed my mistake.
Matt Ellen
A: 

The member in the base class has to be at least protected so it can be visible in the derived class.

Petar Minchev
A: 

try the keyword protected, instead of public/private:

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

DavidM
+40  A: 

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.

Martin Eve
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
+1 Just the answer I was looking for, excellent reply.
Dal
@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
0xA3
@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
@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
+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
A: 

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

Lerxst
+11  A: 

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
+3  A: 

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.

this. __curious_geek
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
Yes, I corrected it. thanks for pointing out the mistake.
this. __curious_geek
A: 

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
Shouldn't Bar derive from Foo?
MPritch
Yep, thanks. I fixed it.
Brian Gideon
A: 

Yes, although heirs cannot access that member.

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

Poni
A: 

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

Ram
A: 

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