Which access modifier is used to allow access to members of a class within the same package but not from outside?

Java MCQ: Which access modifier is used to allow access to members of a class within the same package but not from outside?

a) private
b) protected
c) public
d) default

Answer:

d) default

Explanation:

In Java, when no access modifier is specified for a class member, the default access level is package-private, also known simply as “default.” This means the member is accessible only within the same package but not from outside the package.

This access level is useful when you want to restrict access to certain class members, allowing only classes within the same package to access them. This promotes encapsulation within a package and helps to organize code into logical groupings.

Understanding access modifiers is crucial for controlling visibility and access to class members, thereby protecting the integrity of your code.

Reference links:

https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top