Software Access Modifiers Flashcards

1
Q

Access Modifiers

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why use them?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Choosing Access Modifiers

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly