What are the two access modifier levels?
How do access levels affect programming?
Why use access modifiers?
By making class members private or public, the class interface is clearly separated from the internal implementation of the class. This is called encapsulation. From a security viewpoint, encapsulation stops class members from being changed in an uncontrolled way (e.g. without proper validation, without notifying error handlers, etc).
How to choose access modifiers?
Use the most restrictive access level that makes sense for a particular class member. Use private unless you have a good reason not to. Avoid public fields except for constants which cannot be changed.
Public fields tend to:
- Restrict the code to a particular implementation.
- Limit flexibility in changing the code
What are immutable objects?
An immutable object is an object whose internal state remains constant after it has been created.