The accessibility is there more to decouple implementation details of a
class from the code that uses it. This is to enable an implementor of a
class to change the inner workings of it without having to rewrite all
the code that uses the class. In your example all code that access the
private field is still contained within class A, encapsulation is not
broken.
What you are suggesting sounds more like a security framework that is to
be enforced at runtime. It sure is possible that a language could
support this, but I don't think it belongs there. It is more of an
application feature.
Your post did make me wonder though, does anybody know of any
OO-language that support this kind of accessibility modifier? Or
anything like it?
Regards,
Joakim
WillNap wrote:
It seems odd that a private instance member can be accessed by any other
instance of its defining class. For example:
class A
{
private int x = 1;
public void func (A a)
{
Console.Out.WriteLine ( "private member = {0}", a.x);
}
}
Rather than limit 'private' to an instance, C# allows any instance of A to
have access to another instance's private members - as in the above code.
This would be like allowing any student in an 8th grade math class access to
the exam answers written by any other student. It seems like 'private'
should only allow 'this.x' - not 'a.x' access.
If someone can explain this to me I'd be grateful.