+ 1
Guys, I have been studying classes for two days but up to now i dont get the point of making something private i make all public
2 Respuestas
+ 3
Imagine you write a calendar app that sends you an SMS whenever a friend has their birthday. But sending SMS is hard, so the SMS class is 2000 lines long and there are more variables than you can keep track of.
Thankfully everything is private and only the `sendSMS` function is public!
For one, as you are writing the rest of your calendar app, you don't have to remember what exactly goes on in the SMS class and you can focus on something else. Calling `.sendSMS` is easy enough.
Secondly, as you are writing the SMS class, you can be sure that nobody else is messing with your code. If everything is public, one could set all these complicated variables to nonsense values and break the program accidentally.
And just to be clear, by "someone else" I mean you yourself in a week, you are mostly saving yourself from your own stupidity.
When coding, try to make everything private and only change it to public if you absolutely need to, you will learn to appreciate it once you write longer programs.
+ 1
privacy of defining variable is useful to permit or deny reaching that var. within the class, if you declare all as general it will be reached by all classes.