+ 1
I dont understang, what's the use of making some data private? What's the point? Where is it useful in making a programm?
private data
4 Answers
+ 8
Encapsulation is very important if you've created something that other developers will later work on. Imagine working at Google with thousands of employees, and someone changes 1 variable that later messes up the whole system because it wasn't set to private. HASHTAG PROBLEMS.
+ 2
I think making certain properties and members private applies really well to a situation where other people would be working on the same project/coding environment. You don't want other people to be calling functions on accident that change an object's property values, so you make it private to avoid this problem. I would say if you're just coding on your own, go ahead and make everything public, but if you plan on working as a coder somewhere, it would be beneficial to practice encapsulation.
+ 1
Whatever is declared under private is an implementation detail. You don't need to expose those functions/variables to the users of the class. This also guarantees that no-one outside the class can possibly use the private functions.
This gives you the ability to change the implementation (private members) at a later time without affecting the Interface of the class (public members).
It applies to the real world as well. Imagine interacting with a real car. The interface that the car has provided to you are the gas and brake pedals. The implementation is under the hood, hidden away. You don't need to know what the engine is doing to drive the car. And later the manufacturer could recall your car and put in a electric engine and you shouldn't be affected by it, because you are still using the earlier interface of gas and brake pedals.
0
So the other class can't edit it. Some also can use 'private' in the class constructor so the class can't be instantiated(Or to be created as an object).