Submitted by [deleted] t3_11btu75 in explainlikeimfive
Saporificpug t1_ja00ub0 wrote
Technically nothing would happen. Some languages don't have the concept.
You can think of a program as a list (not a List class) of objects or methods.
For a small grocery list, it's just a list of a few items, you're probably just write them down on a single page with no separation. A small program would be okay with only using one or two classes, it's no big deal.
But say we're talking about multiple store chains in a corporation. You're going to list different stores and different positions in corporate. Each store is going to have their own employees listed, but most people outside of the store is going to be concerned with the store manager and assistant manager so their numbers will be listed for thise outside the store. Inside the store everyone should know about and interact with their coworkers.
Ecapsulation is kind of the same idea. You break code into different classes. Not every class should be concerned with every function or object of a different class, so some objects/methods are marked for different level access so only some or certain objects can interact with those.
One good use of ecapsulation, is imagine you have a method that you have some kind of input like age. A problem with using a public variable from a different class is that you can change the variable and you might not catch every error input like a negative number. By making it private and using a public method that changes it, it can only be changed by that method which can catch if the number is negative, or too high or between a range. This makes debugging easier because the only way that variable could be wrong is if the logic in that setter method is wrong.
Viewing a single comment thread. View all comments