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 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

9th Sep 2019, 2:20 PM
Kisembo Allan
Kisembo Allan - avatar
2 Answers
+ 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.
9th Sep 2019, 3:29 PM
Schindlabua
Schindlabua - avatar
+ 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.
9th Sep 2019, 2:27 PM
Haider Bahaa
Haider Bahaa - avatar