Explain Mutator method

Mutator method: It is a method specifically designed to permit controlled modification of a private attribute of a class. By convention, we name the mutators with a set prefix obeyed by the name of the attribute being transformed. For example, the mutator for an attribute named speed would be setSpeed. By creating an attribute private, we save objects of other classes from modifying its value other than via its mutator. The mutator is capable to check the value being employed to transform the attribute and reject the modification when essential. Moreover, modification of one attribute may need others to be transformed in order to keep the object in a reliable state. A mutator technique can undertake this role. Mutators are employed both to grant safe access to the value of the private attribute and to save attributes from modification by objects of the other classes. The later goal is accomplished by selecting a suitable visibility for the mutator.

   Related Questions in Programming Languages

©TutorsGlobe All rights reserved 2022-2023.