Software Access Modifiers Flashcards
Access Modifiers
Access modifiers set the accessibility of classes, methods and other members. They determine whether other classes can use a particular field or invoke a particular method.
Why use them?
The class interface clearly separates from the internal implementation - called encapsulation.
Security POV - stops class members from being changed in an uncontrolled way without proper validation
Programming POV - changes made to a public class variable could mean having to change the code everywhere
Choosing Access Modifiers
Use the most restrictive access that makes sense for a particular class member.
Use private unless you have a good reason not to; avoid public except for constants which cannot be changed. Public fields restrict the code and limit flexibility.