These two articles are with similar topics, we make them together:
Question:
Can a Private Member be Inherited by Derived Class? --- This is very interesting question.
When I moved into computer science fileld from physical science, I remember I learnt for Inheritance:
- Everything declared as public, internal or protected is inherited, even static member is inheritable, while the private members are also inherited but not accessible.
However, if I search for this topic, even in some interview guide, most of them say that private member are not inherited.
What is the puzzle?
Conclusion:
- private members are inherited but not accessible, except for certain situation.
We discuss C#, while C# is created by Microsoft. Say Microsoft (Tutorial: Introduction to Inheritance - C# | Microsoft Learn):
What is inheritance?
Not all members of a base class are inherited by derived classes. The following members are not inherited:
-
Static constructors, which initialize the static data of a class.
-
Instance constructors, which you call to create a new instance of the class. Each class must define its own constructors.
-
Finalizers, which are called by the runtime's garbage collector to destroy instances of a class.
While all other members of a base class are inherited by derived classes, whether they are visible or not depends on their accessibility. A member's accessibility affects its visibility for derived classes as follows:
- Private members are visible only in derived classes that are nested in their base class. Otherwise, they are not visible in derived classes.
- Protected members are visible only in derived classes.
- Internal members are visible only in derived classes that are located in the same assembly as the base class. They are not visible in derived classes located in a different assembly from the base class.
- Public members are visible in derived classes and are part of the derived class' public interface. Public inherited members can be called just as if they are defined in the derived class.
Demo:
This is a sample code from Microsoft [ref]:
public class A
{
private int _value = 10;
public class B : A
{
public int GetValue()
{
return _value;
}
}
}
public class C : A
{
// public int GetValue()
// {
// return _value;
// }
}
public class AccessExample
{
public static void Main(string[] args)
{
var b = new A.B();
Console.WriteLine(b.GetValue());
}
}
// The example displays the following output:
// 10
A private field is defined at Line 3 in class A, class C is inherited from class A, if we take off the commend out linew from Line 16-19 above, there will be an error, like:
- "A._value" is inaccessable due to its protection level.
It is saying, the private filed A._value is inherited, but not accessable.
On the other hand, class B (Line 5) is defined inside class A, and also inherited from class A. class B can read the A._value (the value from parent class).
![]()
Comment out the error line in class C, run the program we got
![]()
That is saying, the private field A._value is inherited by class B and can be accessable by class B, the nested class inside A.
References: