QWhich keyword can protect a class in a package from accessibility by the classes outside the package ?
Question Info
Choose the Best Option
Click any option to instantly check if you're correct.
Explanation
Accessibility of classes in Java
-
In Java, top-level classes (i.e., not inner classes) can only have:
-
public→ accessible from anywhere. -
default (no modifier) → accessible only within the same package.
-
-
privateandprotectedcannot be used with a top-level class. -
finalmeans 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!
-
A default (package-private) class is accessible only within the same package.
-
Classes outside the package cannot access it.
✅ Answer: (d) don’t use any keyword at all (make it default)
Share This Question
Challenge a friend or share with your study group.