Bird
Raised Fist0

You are designing a banking application where account balance must never be set to a negative value. Which encapsulation approach best supports this requirement?

hard🌍 Real-world Scenario Q9 of Q15
OOP & Design Patterns - Encapsulation - Data Hiding, Getters/Setters & Access Modifiers
You are designing a banking application where account balance must never be set to a negative value. Which encapsulation approach best supports this requirement?
AMake balance public and rely on external code to validate
BProvide only a public getter and no setter for balance
CUse protected balance field accessible by subclasses for validation
DUse a private balance field with a setter that validates input before updating
Step-by-Step Solution
Solution:
  1. Step 1: Identify encapsulation for validation

    Private field with setter validation ensures balance cannot be set negative internally.
  2. Final Answer:

    Option D -> Option D
  3. Quick Check:

    Setter validation enforces business rules [OK]
Quick Trick: Setters enforce validation rules internally [OK]
Common Mistakes:
MISTAKES
  • Making balance public trusting external validation
  • Using getter-only which prevents updates
  • Using protected exposing to subclasses without control
Trap Explanation:
PITFALL
  • Candidates may underestimate importance of internal validation via setters.
Interviewer Note:
CONTEXT
  • Evaluates ability to apply encapsulation to enforce invariants.
Master "Encapsulation - Data Hiding, Getters/Setters & Access Modifiers" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes