So i was browsing some code and i came across:
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!";
}
}
}
Can someone please explain how the private variable "message" is being used/accessed even though its private??
Private members are accessible to all code within the class, including nested classes.
If you move the Employee
class outside the Person
class, it will fail until you make the field protected
.