I don’t think I’m understanding why setting something to private other than “you should just do it”. If I had a vending machine class and it has a variable that says how many coins it has in the machine which is useless to everything else, why should I set it to private? It’s not like I’m going to get high and in another class write vendingMachine.coins = 99999.
I don’t think I’m understanding why setting something to private other than you should
Share
That’s probably true, but the point in OOP is to separate responsibilities and to keep data together with the corresponding methods. For very small and one man projects this might not be necessary, but in big projects where you reuse objects from other people you will sometimes not know which fields you should modify and which should not be messed with. That’s why in OOP you define only the interface public and all internal stuff private. That also allows you to change the internal implementation later without breaking your program.
Have a look:
http://simple.wikipedia.org/wiki/OOP