What are access modifiers in Java?
Why Interviewers Ask This
This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Java topics. It also reveals how well you can explain technical ideas to non-experts.
Answer
Java has four access modifiers that control the visibility of classes, methods, and fields. private: accessible only within the same class. default (no modifier): accessible within the same package (package-private). protected: accessible within the same package and by subclasses in other packages. public: accessible from everywhere. The general best practice is to use the most restrictive modifier possible — private for fields (accessed via getters/setters), public only for the API that other classes need to use. This principle of least privilege is fundamental to encapsulation.
Common Mistake
Don't just define the term — demonstrate that you understand when to use it and when not to. Showing awareness of trade-offs is what separates average from strong Java candidates.
Previous
What is the difference between abstract class and interface in Java?
Next
What does the static keyword mean in Java?