Note: this article is published on 07/23/2024.
These two articles are with similar topics, we make them together:
- Can a Private Member be Inherited by Derived Class? --- this article
- Can a Static Member be Inherited by Derived Class?
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:



Join the conversation! Your thoughts help the community grow.