How does encapsulation protect an object’s rules? Explain using a bank account.
Interactive Practice Mode
Formulate your thoughts or take notes before revealing the model solution.
Saved automatically
Model Answer & Deep-Dive Explanation
Encapsulation places state and the operations that control it behind a defined interface. An account should expose deposit and withdrawal methods rather than a freely writable balance. Those methods can reject negative amounts, enforce an overdraft rule, and record transactions. A private field alone is insufficient if a public setter allows arbitrary balances. The goal is to preserve valid states and reduce the number of places that must change when a business rule changes. Monetary values should use an appropriate decimal or minor-unit representation.
Related Questions in Software Engineering & OOP
Explain the SOLID Principles of Object-Oriented Software Engineering with Examples
Intermediate level • 683 views
Explain ACID properties in relational databases and how different Transaction Isolation Levels prevent concurrency anomalies.
Intermediate level • 206 views
How do abstraction and inheritance solve different problems?
Easy level • 62 views
When would you use composition instead of inheritance for retry behavior?
Medium level • 62 views
A notification class has growing email, SMS, and push conditionals. How would you refactor it?
Medium level • 60 views