Bird
0
0

You want to keep a user's password private in a Java class but allow other classes to check if a password matches. Which is the best way to do this?

hard📝 Application Q15 of 15
Java - Packages and Access Control
You want to keep a user's password private in a Java class but allow other classes to check if a password matches. Which is the best way to do this?
AMake the password field private and provide a public method to check the password.
BMake the password field public so other classes can read it directly.
CMake the password field protected and access it from other classes.
DMake the password field private and provide a public method to return the password.
Step-by-Step Solution
Solution:
  1. Step 1: Protect sensitive data with private fields

    Passwords should be private to prevent direct access and protect security.
  2. Step 2: Provide controlled access via public method

    A public method can check if a given password matches without revealing the actual password.
  3. Final Answer:

    Make the password field private and provide a public method to check the password. -> Option A
  4. Quick Check:

    Private data + public check method = secure design [OK]
Quick Trick: Keep data private; use public methods to interact safely [OK]
Common Mistakes:
  • Making password public exposes sensitive data
  • Returning password directly breaks security
  • Using protected exposes data to subclasses unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes