Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can private member variable be changed by class instance?

class TestClass {     private string _privateString = "hello";     void ChangeData()     {         TestClass otherTestClass = new TestClass();         otherTestClass._privateString = "world";     } } 

This code compiles in C# and the equivalent works in PHP, but can someone explain the reason why otherTestClass._privateString can be changed here ?

I would have thought an instance of a class should not be able to change a private member variable under any circumstances, and that trying to access otherTestClass._privateString would give an 'inaccessible due to protection level' error.

This is not the case though, so why does instantiating an object inside its own class let you access the private members ? And should it, doesn't this break encapsulation to an extent ? Or am I missing something obvious ?

  • (I'm not asking if the above class design is good practice, just wondering about the theory behind it.)

Edit - Thanks for the answers and comments. To clarify, I'm also interested in knowing if being able to do this is regarded as a positive feature, or if it's a necessary tradeoff for better compile-time checking/code clarity/because most other languages do it that way or whatever. It seems to me ideally the compiler would prevent or warn you about this, but then I'm far from a language designer. Any examples of how it being this way lets you do something useful (without violating encapsulation) that would otherwise be difficult or impossible would be great.

like image 809
Michael Low Avatar asked Nov 22 '10 11:11

Michael Low


People also ask

Can we change value of private variable?

the way to set the value of private variable is by setter and getter methods in class. Show activity on this post. you can call method main() in method something().

Can private variables be modified in Java?

Yes. By using reflection, you can access your private field without giving reference methods.

Can a private variable be accessed from another class?

We can access a private variable in a different class by putting that variable with in a Public method and calling that method from another class by creating object of that class.

Why do we make variables private in classes?

Making a variable private "protects" its value when the code runs. At this level, we are not concerned with protecting it from other programmers changing the code itself. The point of so-called "data hiding" is to keep internal data hidden from other classes which use the class.


2 Answers

Private members are accessible to any code within the program text of that class (including within nested types). It has nothing to do with which instance of the class you're dealing with.

I don't believe this violates encapsulation - the API is still separated from the implementation, but the implementation "knows" about itself regardless of which instance it's looking at.

I believe that in some other languages this isn't how accessibility works, but it definitely is for C# and Java. (Java has slightly different rules about what can access private members, but the translated code for what you've written would still work.)

like image 117
Jon Skeet Avatar answered Oct 11 '22 14:10

Jon Skeet


This is because C# enforces class-level privacy and not object-level privacy.

Most mainstream languages enforce the same policy, i.e. C#, C++ and Java. I think the reason are:

1) because developers are accustomed to that kind of policy;

2) because object-level privacy would become much too tedious in return of very few advantages.

like image 24
Simone Avatar answered Oct 11 '22 16:10

Simone