access modifier interview questions:
Basic Concepts
1. What are the four access modifiers in Java?
-
public
– Accessible everywhere. -
private
– Accessible only within the same class. -
protected
– Accessible within the same package and by subclasses (even in different packages). - Default (package-private) – Accessible only within the same package (no explicit keyword).
2. Explain the differences between public
, private
, protected
, and default access.
| Modifier | Class | Package | Subclass (same pkg) | Subclass (diff pkg) | World (anywhere) |
|--------------|-------|---------|---------------------|---------------------|------------------|
| public
| ✅ | ✅ | ✅ | ✅ | ✅ |
| protected
| ✅ | ✅ | ✅ | ✅ | ❌ |
| Default | ✅ | ✅ | ✅ | ❌ | ❌ |
| private
| ✅ | ❌ | ❌ | ❌ | ❌ |
3. What is the default access modifier in Java when none is specified?
- Default (package-private) – Only accessible within the same package.
4. Can you access a private method from another class?
-
No,
private
methods are accessible only within the same class.
Practical Application
5. When would you use private
vs. public
access modifiers?
- Use
private
for encapsulation (hiding internal implementation). - Use
public
for methods/classes that need to be accessible globally (e.g., API methods).
6. Why would you make a class member protected
instead of public
or private
?
-
protected
allows subclasses to access the member while still restricting access from unrelated classes.
7. How does default (package-private) access differ from protected
?
- Default: Only accessible within the same package.
- Protected: Accessible within the same package and by subclasses (even in different packages).
8. Can a subclass access its superclass's private
members?
-
No, but it can access
protected
andpublic
members.
Advanced Topics
9. Can access modifiers be applied to local variables?
- No, local variables (inside methods/blocks) cannot have access modifiers. They are always local in scope.
10. How do access modifiers affect method overriding?
- The overriding method cannot be more restrictive than the overridden method (e.g., cannot override a
public
method asprivate
).
11. What happens if you try to override a method with a more restrictive access modifier?
-
Compilation error (e.g., overriding a
public
method asprivate
is invalid).
12. Can interfaces have private
methods? (Java 9+)
-
Yes, Java 9+ allows
private
methods in interfaces (for internal helper methods).
13. How do access modifiers work with nested classes?
- Nested classes (
static
or non-static
) can have any access modifier (public
,private
,protected
, default).
Scenario-based Questions
14. If you want a method to be accessible only within its package and to subclasses (even in other packages), which access modifier would you use?
-
protected
(since default only allows same-package access).
15. Why might you declare a class as package-private (no modifier)?
- To restrict usage to the same package (e.g., utility classes not meant for external use).
16. How would you expose certain functionality of a class while keeping its implementation details hidden?
- Use
public
methods for external access andprivate
methods for internal logic (encapsulation).
Tricky Questions
17. Can a public
class have private
or protected
members?
-
Yes, a
public
class can have any access modifier for its members (e.g.,private
fields,protected
methods).
18. Is it possible to restrict access to a public
method in a subclass?
-
No, you cannot make it more restrictive (e.g., cannot override
public
asprivate
).
19. Can you change the access modifier when overriding a method?
-
Yes, but only to make it less restrictive (e.g., overriding a
protected
method aspublic
is allowed).
20. Why can't a subclass method be more restrictive than its superclass method?
- It would violate the Liskov Substitution Principle (a subclass should be usable wherever the superclass is expected).
Bonus: Common Mistakes
-
Assuming
protected
means only subclasses → It also allows same-package access. -
Using
default
whenprotected
is needed →default
doesn’t allow subclass access from different packages. - Trying to use access modifiers on local variables → Not allowed.
Would you like a deeper explanation on any specific topic? 😊