Consider a scenario where you have a class representing a "User" with a field "password". How would you ensure that the password field is securely encapsulated and cannot be directly accessed or modified without proper validation?

  • Make the password field private and provide public getter and setter methods with validation checks in the setter.
  • Make the password field protected and provide public getter and setter methods with validation checks in the setter.
  • Make the password field public with proper validation checks inside the setter method.
  • Use the final keyword with the password field.
To ensure the password field is securely encapsulated, it should be made private. Public getter and setter methods should be provided, allowing controlled access and validation checks inside the setter to prevent unauthorized access or modification of the password. Making the field public or protected would expose it directly, which is not secure. Using final does not provide encapsulation.
Add your answer
Loading...

Leave a comment

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