Q: Which keyword can protect a class in a package from accessibility by the classes outside the package ?
-
A
private
-
B
protected
-
C
final
-
D
don't use any keyword at all (make it default)
D
Answer:
D
Explanation:
Accessibility of classes in Java
-
In Java, top-level classes (i.e., not inner classes) can only have:
-
private and protected cannot be used with a top-level class.
-
final means the class cannot be inherited, but it doesn’t restrict access across packages.
Now checking the options
(a) private → ❌ Not allowed for a top-level class.
(b) protected → ❌ Not allowed for a top-level class (only applies to members).
(c) final → ❌ Only prevents subclassing, not accessibility.
(d) don’t use any keyword at all (make it default) → ✅ Correct!
✅ Answer: (d) don’t use any keyword at all (make it default)
Related Topic:
Share Above MCQ