|
||
| Controlling access
If you lack experience with object-oriented programming, you may not immediately see the significance of access modifiers.
The significance of access modifiers has to do with one of the main premises behind OOP, which is the encapsulation of data and behavior within a single construct, a class. It is very important for a class to manage its data internally and restrict access to other classes. In other words, an outside class must call methods on a class to alter the class's state, as opposed to simply modifying the class's member variables directly. Access modifiers make this possible because they allow you to restrict the access to member variables. They also go a step further and allow you to restrict the access to methods. This restriction is useful in situations where you may have a helper method that is only used within the inner workings of a class. In this case, you wouldn't want other classes to be able to call the method. Java classes enter the access equation in a sense that you can restrict the access to an entire class. This is useful in situations where you may have a helper class that is only used by classes in a certain package. |
||
|
|
||
