Method (computer Science) - 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 Science)

Famous quotes containing the word methods:

    The reading public is intellectually adolescent at best, and it is obvious that what is called “significant literature” will only be sold to this public by exactly the same methods as are used to sell it toothpaste, cathartics and automobiles.
    Raymond Chandler (1888–1959)