Java: Getters and Setters Design
Why might exposing a public getter for sensitive data be risky?
Sensitive data could be accessed and misused. Exposing a public getter for sensitive data, like a password, can…
View Card →Quick study sessions to strengthen memory and retain key concepts.
Why might exposing a public getter for sensitive data be risky?
Sensitive data could be accessed and misused. Exposing a public getter for sensitive data, like a password, can…
View Card →How does immutability support encapsulation?
Immutability prevents changes to the object's state after creation. Immutable objects are fully initialized upon creation and their…
View Card →Why does validation in setters matter in this design?
To ensure only valid data is assigned to fields. Validation in setters prevents invalid data from corrupting the…
View Card →Why prefer intentional design over generated boilerplate?
Intentional design ensures the class fits its purpose and maintains integrity. Relying on auto-generated getters and setters can…
View Card →What are the tradeoffs of a large API surface?
It increases the complexity and maintenance burden. A large API surface can make a class more difficult to…
View Card →How do domain methods differ from generic setters?
Domain methods encapsulate specific logic, whereas setters do not. Domain methods provide a specific way to interact with…
View Card →Why might you avoid creating setters for every field?
To maintain control over how and when a field is modified. Not every field needs a setter, especially…
View Card →When is a getter method reasonable to use?
When you need to provide read access to a field without exposing it directly. Getters are used to…
View Card →Why are private fields common in Java classes?
To enforce encapsulation by restricting direct access. Private fields are used to protect the internal state of an…
View Card →Why is a domain method often better than a generic setter?
A domain method can explain the action and validate it more safely. A generic setter like `setStock(...)` exposes…
View Card →