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.
Loading...
Related Quiz
- What will be the outcome if you try to execute a DML (Data Manipulation Language) operation using executeQuery() method?
- Which operator can be used to invert the sign of a numeric expression?
- Considering a real-world scenario where a thread pool is being used to manage multiple client requests to a server, what could be the potential issues if the thread pool size is too small or too large? How would you determine an optimal thread pool size?
- The collect() method in the Stream API is a type of ________ operation.
- The @FunctionalInterface annotation is used to indicate that an interface is to be used with ________.