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 ?
Correct answer: C. Do not declare the method with any accessibility modifiers.
- A. Declare the method with the keyword public.
- B. Declare the method with the keyword private.
- C. Do not declare the method with any accessibility modifiers.
- D. Declare the method with the keyword public and private.
Explanation
A method with no access modifier has package-private access, so it can be used by classes in the same package but not by classes in other packages. `public` is widely accessible, while `private` restricts access to the declaring class.
Report an error
The more specific you are, the faster it gets fixed. A source beats an opinion.
Prefer email? [email protected]
Practise Software Design and Architecture
493 free Software Design and Architecture MCQs from Software Engineering, each with the correct answer and an explanation. Unlimited attempts, no account needed.
Discussion
Stuck on an option, or know a faster way to get there? Ask or explain it here.
No comments yet. Be the first to explain this one.
Exams that ask Software Engineering questions like this
Software Engineering is on this paper prepared for on TestUstad, and all of them draw the same bank, so this question is worth knowing for it.