Chapter 10: Problem 3
Why is an object's internal data usually hidden from outside code?
Short Answer
Expert verified
Answer: An object's internal data is usually hidden from outside code to promote encapsulation, which is the idea of bundling an object's data and its methods within a single unit. Hiding internal data helps achieve modularity, allowing objects to be developed, maintained, and updated independently. This approach prevents unwanted modification of data, abstracts away implementation details, makes the code easier to maintain and modify, and increases code robustness. For instance, in a bank account object, hiding internal data like the account balance ensures that only the object's methods, such as deposit and withdraw, can access and modify the balance through proper validation checks.