Chapter 6: Problem 15
What is an accessor? What is a mutator?
Short Answer
Expert verified
An accessor, or getter method, is a function in a class or object that retrieves the value of a private attribute without directly accessing it. It helps maintain data integrity and security by promoting encapsulation and data hiding in object-oriented programming. A mutator, or setter method, is a function in a class or object that modifies the value of a private attribute by validating its input data to maintain data integrity and security. Both accessors and mutators prevent unauthorized access to an object's data. Examples of an accessor and mutator can be seen in Python's `get_name()` and `set_name()` methods in the Student class, where the private attribute `__name` is retrieved or modified without direct access.