Originally Posted by
6StringJazzer
For example, suppose we have an object with an attribute that defines the state of the object, like the balance of a bank account. That attribute is encapsulated--it is private to the object, and the object has exclusive rights to manage that state. If external code needs to know that state, the object provides a function to return its value, and does not allow those entities to explicitly change it. Any change to the state must occur as a result of what methods are called and what inputs are provided (MakeDeposit, MakeWithdrawal, CreditInterest). However, if that attribute were to be made public for the convenience of a programmer who doesn't want to be bothered by writing a call to a function (or thinks it creates an efficiency issue), then the object loses control over its own state. It creates an opportunity to change the state that falls outside the rules defined by the object. This creates bugs that are hell to diagnose.
Bookmarks