Method (computer Programming) - Accessor and Mutator Methods

Accessor and Mutator Methods

An 'accessor' method is a method that is usually small, simple and provides the sole means for the state of an object to be accessed (retrieved) from other parts of a program.

Although this introduces a new dependency, as stated above, use of methods is preferred, in the object-oriented paradigm, to directly accessing state data - because those methods provide an abstraction layer. For example, if a bank-account class provides a getBalance accessor method to retrieve the current balance (rather than directly accessing the balance data fields), then later revisions of the same code can implement a more complex mechanism for balance retrieval (say, a database fetch), without the dependent code needing to be changed (However, this often claimed advantage is not unique to object oriented programming, and was earlier implemented - when desirable in critical systems - through conventional modular programming with optional run-time, system-wide locking mechanisms, in the imperative/procedural paradigms)
To compare the value of two data items, two accessor-method calls are normally required before a comparison can take place between the retrieved primitive data type values. Comparator methods are required to compare entire objects for equality. This contrasts with a direct comparison in non-OOP paradigms. An update, modifier, or mutator method, is an accessor method that changes the state of an object. Objects that provide such methods are considered mutable objects.

Read more about this topic:  Method (computer Programming)

Other articles related to "accessor and mutator methods, accessor, method, methods":

Method (computer Science) - Accessor and Mutator Methods
... An 'accessor' method is a method that is usually small, simple and provides the sole means for the state of an object to be accessed (retrieved) from other parts of a program ... introduces a new dependency, as stated above, use of methods is preferred, in the object-oriented paradigm, to directly accessing state data - because ... For example, if a bank-account class provides a getBalance accessor method to retrieve the current balance (rather than directly accessing the balance data fields), then later revisions of the ...

Famous quotes containing the word methods:

    All good conversation, manners, and action, come from a spontaneity which forgets usages, and makes the moment great. Nature hates calculators; her methods are saltatory and impulsive.
    Ralph Waldo Emerson (1803–1882)