Accessor methods are used to read the data values of an object. Mutator methods are used to modify the data of an object. Manager methods are used to initialize and destroy objects of a class, e.g. constructors and destructors. These methods provide an
abstraction layer that facilitates
encapsulation and
modularity. 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 (e.g., a
database fetch), without the dependent code needing to be changed. The concepts of encapsulation and modularity are not unique to object-oriented programming. Indeed, in many ways the object-oriented approach is simply the logical extension of previous paradigms such as
abstract data types and
structured programming.
Constructors A
constructor is a method that is called at the beginning of an object's lifetime to create and initialize the object, a process called
construction (or
instantiation). Initialization may include an acquisition of resources. Constructors may have parameters but usually do not return values in most languages. See the following example in Java: public class Person { private String name; private int age; // constructor method public Person(String name, int age) { this.name = name; this.age = age; } }
Destructor A
Destructor is a method that is called automatically at the end of an object's lifetime, a process called
destruction. Destruction in most languages does not allow destructor method arguments nor return values. Destructors can be implemented so as to perform cleanup chores and other tasks at object destruction.
Finalizers In
garbage-collected languages, such as
Java,
C#, and
Python, destructors are known as
finalizers. They have a similar purpose and function to destructors, but because of the differences between languages that utilize garbage-collection and languages with
manual memory management, the sequence in which they are called is different. ==Abstract methods==