A Method is a subroutine attached to a specific class defined in the source code of a program. It is similar to a function, but can only be called by an object created from a class. Methods are an important part of object-oriented programming since they isolate functions to individual objects. The methods within a class can only be called by objects created from the class. Additionally, methods can only reference data known to the corresponding object. This helps isolate objects from each other and prevents methods within one object from affecting other objects. While methods are designed to isolate data, they can still be used to return values to other classes if necessary. If a value needs to be shared with another class, the return statement can be used.
The idea of methods appears in all object-oriented programming languages. Methods are similar to functions or procedures in other programming languages such as C, SQL and Delphi. An object method can only have access to the data known by that object. This maintains the integrity of data between sets of objects in a program. A method can be reused in many objects. As a simple example, let's say that a module has a VideoClip object that handles functions related to movie clips. The VideoClip object would probably have some of the following methods:
- Play: Begin playing the movie clip.
- Pause: Pause the movie clip.
- Stop: Stop playing the movie clip.
Accessor, Mutator and Manager Methods
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.
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:
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. Destruction can be implemented so as to perform cleanup chores and other tasks at object destruction.
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.
An abstract method is one with only a signature and no implementation body. It is often used to specify that a subclass must provide an implementation of the method. Abstract methods are used to specify interfaces in some programming languages.
The following Java code shows an abstract class that needs to be extended:
The following subclass extends the main class:
If a subclass provides an implementation for an abstract method, another subclass can make it abstract again. This is called reabstraction.
In practice, this is rarely used.
In C#, a virtual method can be overridden with an abstract method. (This also applies to Java, where all non-private methods are virtual.)
Interfaces' default methods can also be reabstracted, requiring subclasses to implement them. (This also applies to Java.)
Class methods are methods that are called on a class rather than an instance. They are typically used as part of an object meta-model. I.e, for each class, defined an instance of the class object in the meta-model is created. Meta-model protocols allow classes to be created and deleted. In this sense, they provide the same functionality as constructors and destructors described above. But in some languages such as the Common Lisp Object System (CLOS) the meta-model allows the developer to dynamically alter the object model at run time: e.g., to create new classes, redefine the class hierarchy, modify properties, etc.
Special methods are very language-specific and a language may support none, some, or all of the special methods defined here. A language's compiler may automatically generate default special methods or a programmer may be allowed to optionally define special methods. Most special methods cannot be directly called, but rather the compiler generates code to call them at appropriate times.
Static methods are meant to be relevant to all the instances of a class rather than to any specific instance. They are similar to static variables in that sense. An example would be a static method to sum the values of all the variables of every instance of a class. For example, if there were a Product class it might have a static method to compute the average price of all products.
In Java, a commonly used static method is:
This static method has no owning object and does not run on an instance. It receives all information from its arguments.
A static method can be invoked even if no instances of the class exist yet. Static methods are called "static" because they are resolved at compile time based on the class they are called on and not dynamically as in the case with instance methods, which are resolved polymorphically based on the runtime type of the object.
Copy-assignment operators Copy-assignment operators define actions to be performed by the compiler when a class object is assigned to a class object of the same type.
Operator methods define or redefine operator symbols and define the operations to be performed with the symbol and the associated method parameters. C++ example: