Interview Q&A · Java · 6 questions in this topic
Access Modifiers for Classes, Methods and Variables — Java Interview Q&A
How the four access levels control visibility for classes, methods, and fields. Part of OOP Mechanics · 294 questions in the Java bank.
Which access modifiers are allowed on a top-level class?
Answer
Only public and package-private, written by omitting any modifier, are allowed on a top-level class. A public top-level class is visible everywhere, while a package-private class is visible only inside its own package. Adding private or protected is illegal, although non-access modifiers such as final, abstract, and sealed are permitted.
What are access modifiers in Java?
Answer
Access modifiers are keywords that enforce encapsulation by controlling where a class, method, constructor, or field can be used. Java has four access levels: private, package-private with no keyword, protected, and public, from most to least restrictive. Choosing the narrowest level that works keeps internals hidden and prevents misuse.
Key points
- 1private is visible only inside the same class.
- 2package-private is visible only inside the same package.
- 3protected adds subclasses in other packages, and public is visible everywhere.
Access specifiers vs access modifiers - is there a difference in Java?
Answer
In Java there is no separate category called access specifiers, so the Java Language Specification calls them all modifiers. Access modifiers are public, protected, private, and the default package-private level, while non-access modifiers include static, final, abstract, synchronized, and similar keywords. The specifier versus modifier split comes from C++, so it does not apply to Java.
Which access modifiers can a class use?
Answer
A top-level class can be public or package-private only. A public class is usable from any package, subject to module exports when modules are used, while a package-private class is usable only inside its own package. Member and nested classes are different because they live inside another class and can use all four levels.
Which access modifiers can methods use?
Answer
Methods can use all four access levels. A public method is callable from anywhere, a protected method is callable inside its package and from subclasses in other packages, a package-private method is callable only inside its own package, and a private method is callable only inside the same class. Constructors follow the same rules, which is how utility classes and singletons restrict instantiation.
Which access modifiers can variables (fields) use?
Answer
Fields can use all four access levels with the same visibility table as methods. Public fields are reachable everywhere, protected and package-private fields are reachable inside the package with protected also reaching subclasses elsewhere, and private fields are reachable only inside the declaring class. Good design keeps fields private or narrowly scoped and exposes them through methods so invariants can be enforced.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission