I dont understang, what's the use of making some data private? What's the point? Where is it useful in making a programm? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

24th Feb 2017, 3:51 PM
mohammad mazhariroshan
mohammad mazhariroshan - avatar
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.
24th Feb 2017, 7:38 PM
Mark Foxx
Mark Foxx - avatar
+ 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.
24th Feb 2017, 4:53 PM
Zeke Williams
Zeke Williams - avatar
+ 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.
1st Mar 2017, 3:48 AM
Vimal Krishna
Vimal Krishna - avatar
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).
24th Feb 2017, 4:08 PM
Marcellino Chris O'vara
Marcellino Chris O'vara - avatar