Possible Duplicate:
Why are private fields private to the type, not the instance?
Consider the following class:
public class Test
{
protected string name;
private DateTime dateTime;
public Test(string name)
{
this.name = name;
this.dateTime = DateTime.Now;
}
public Test(Test otherObject)
{
this.name = otherObject.name;
this.dateTime = otherObject.GetDateTime();
}
private DateTime GetDateTime()
{
return dateTime;
}
public override string ToString()
{
return name + ":" + dateTime.Ticks.ToString();
}
}
In my constructor I'm calling private or protected stuff of the otherObject
. Why is this possible? I always thought private was really private (implying only the object could call that method / variable) or protected (only accessible by overloads).
Why and when would I have to use a feature like this?
Is there some OO-logic / principle that I'm missing?