Real example for access identifiers [public, protected, private]
design-patterns, java, oop
Solution
Here is your answer: https://softwareengineering.stackexchange.com/questions/143736/why-do-we-need-private-variables
In short, it is a matter of maintaining complexity and reusability in the long run. By ensuring that variables are private to a class, you can stop fellow programmers (or even your future self) from modifying anything major that affects the internals of your classes.
As for a real world example, imagine your future self sitting down with some code you wrote and trying to locate where a certain bug occurs. After days of hunting you determine the culprit: A member variable of an instance of your class was modified. And not even by you, but by a fellow member of your team who depended on that variable.
Now you have a big problem. Private variables save you from all this trouble.
Problem
I am new to OOP. I know that there are three identifiers and they should be used in different situations. I have also read lots of discussion regarding whether or not it is dangerous to use the "public" identifier too causally. But I don't really understand why this is dangerous. Let say I have a windows application or web application. In those applications I declared some public methods or variables, how is that dangerous? I mean, my applications accept user input and then produce output, so in what ways can it be dangerous? How can others or other programs attack or take advantages or somehow do some damage to the application because of the "public" identifier. Can anyone describe a real life example? Thanks.