What are access modifiers in Java
๐ก Concept: Access Modifiers in Java
Access modifiers define the visibility and accessibility of classes, methods, and variables in Java.
๐ Quick Intro
Java has four main access modifiers: public, private, protected, and default (package-private), which control access levels.
๐ง Analogy
Think of access modifiers as privacy settings on social media, controlling who can see your posts (members).
๐ง Technical Explanation
- public: Accessible from anywhere.
- private: Accessible only within the class.
- protected: Accessible within package and subclasses.
- default: Accessible within the package.
- Used to enforce encapsulation and hide implementation details.
๐ฏ Use Cases
- โ Use private to hide internal details.
- โ Use public for API methods.
- โ Use protected to allow subclass access.
- โ Use default for package-level access.
๐ป Java Access Modifiers Example
public class Example {
public int publicVar = 1;
private int privateVar = 2;
protected int protectedVar = 3;
int defaultVar = 4; // package-private
public void display() {
System.out.println("Public: " + publicVar);
System.out.println("Private: " + privateVar);
System.out.println("Protected: " + protectedVar);
System.out.println("Default: " + defaultVar);
}
}

โ Interview Q&A
Q1: What are the four access modifiers in Java?
A: public, private, protected, default.
Q2: Which modifier is most restrictive?
A: private.
Q3: What is default access?
A: Package-private.
Q4: Can protected members be accessed outside package?
A: Yes, in subclasses.
Q5: Can private members be accessed outside class?
A: No.
Q6: Why use access modifiers?
A: To implement encapsulation.
Q7: Can classes be private?
A: Only nested classes.
Q8: Can interface members be private?
A: Yes, from Java 9.
Q9: What happens if no access modifier is specified?
A: Default (package) access is used.
Q10: What is encapsulation?
A: Hiding internal details using access modifiers.
๐ MCQs
Q1. What are the four access modifiers in Java?
- public, private, protected, default
- public only
- private only
- protected only
Q2. Which modifier is most restrictive?
- public
- private
- protected
- default
Q3. What is default access?
- public
- private
- Package-private
- protected
Q4. Can protected members be accessed outside package?
- No
- Yes, in subclasses
- Only in package
- Never
Q5. Can private members be accessed outside class?
- Yes
- No
- Sometimes
- Only with reflection
Q6. Why use access modifiers?
- To increase visibility
- To implement encapsulation
- To speed up code
- For inheritance
Q7. Can classes be private?
- Yes
- No
- Only nested classes
- Only abstract classes
Q8. Can interface members be private?
- No
- Yes, from Java 9
- Only public
- Only protected
Q9. What happens if no access modifier is specified?
- public
- private
- Default (package) access
- protected
Q10. What is encapsulation?
- Exposing all details
- Hiding internal details using access modifiers
- Inheritance
- Polymorphism
๐ก Bonus Insight
Proper use of access modifiers is key to maintaining robust, secure, and maintainable Java code.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!