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 • 623 views
Explain ACID properties in relational databases and how different Transaction Isolation Levels prevent concurrency anomalies.
Intermediate level • 152 views
How do abstraction and inheritance solve different problems?
Easy level • 5 views
A notification class has growing email, SMS, and push conditionals. How would you refactor it?
Medium level • 5 views
What is the Liskov substitution principle, and why can a square-as-rectangle hierarchy violate it?
Medium level • 4 views