63.6k views
0 votes
A method within a class is only accessible by classes that are defined within the same package as the class of the method. Which one of the following is used to enforce such restriction?

(a) Declare the method with the keyword public
(b) Declare the method with the keyword private
(c) Declare the method with the keyword protected
(d) Do not declare the method with any accessibility modifiers
(e) Declare the method with the keyword public and private.

1 Answer

4 votes

Final answer:

The correct way to restrict method access within the same package is by not declaring it with any accessibility modifiers, which gives it default package-level access.

Step-by-step explanation:

When a method within a class is supposed to be accessible only by classes that are defined within the same package, the correct way to enforce such a restriction is to use default access modifier. In Java, this is achieved by not declaring the method with any accessibility modifiers (option d). So, if you don't specify public, private, or protected in front of a class member, it will have package-level access by default, meaning it can only be accessed within its own package.

User Dvanrensburg
by
8.7k points

No related questions found