Are private members also inherited?
The following code shows that private members are also inherited.
using System; class A { private int a; } class B : A { } class Program { static void Main(string[] args) { B obB = new B(); A obA = new A(); //This is a proof that a is also inherited. See the error message Console.WriteLine(obB.a);//A.a is inaccessible due to its protection level Console.WriteLine(obB.b);//'B' does not contain a definition for 'b' and no extension Console.WriteLine(obA.b);//'A' does not contain a definition } }
We have encountered two different types of errors: CS0122 and CS1061.
Error | Meaning |
---|---|
CS0122 | A.a is inaccessible due to its protection level. It indicates that the private member a from class A is inherited in the child class B. |
CS1061 | field is not present. |