Software Access Modifiers Flashcards
What are the two access modifier levels?
- Top level (class level) – can use public
- Member level (inside class) – can use public, private, protected
How do access levels affect programming?
- When using classes that come from another source, access levels determine which members of those classes your own classes can use.
- When writing a class, you need to decide what access level every member variable and every method in you class should have.
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.