Private Keyword

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
Well, you'll get a lot of answers on this one...

A short answer is that object based permissions aren't easy to manage
in a static typed system.

Also, often, when one object works with another of its type (as you've
shown above) its for Parameter initialization. This would require some
type of private access...

-Mezz
 
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
 
All,

Thanks for your remarks. It seems that John Miles said it best:
"Data-hiding is an OOP convention, not a security feature."

WillNap
 
Will,

I don't agree with you. A simple sample, there will become probably be a lot
of problems using a public with a lower case starting field with a same name
with an uper case starting public property.

The generated dll's are even not proper for general use in Net

Just my thought,

Cor
 
Cor,

I agree with your point. John Miles, I think, was speaking to a perspective
of OOP data hinding in general.

Will
 
Back
Top