Table of Contents
Method (computer programming)
Assembly
- Assembly - Assembly language - Assembly programming language - Assembly glossary - Assembly terms - Assembly term - Assembly programming glossary - Assembly programming language glossary - Glossary of Assembly - Glossary of Assembly language - Glossary of Assembly programming - Glossary of Assembly programming language - Glossary of Assembly programming language terms
Bash
- Bash - Bash language - Bash programming language - Bash glossary - Bash terms - Bash term - Bash programming glossary - Bash programming language glossary - Glossary of Bash - Glossary of Bash language - Glossary of Bash programming - Glossary of Bash programming language - Glossary of Bash programming language terms
C
C++
- C++ - C++ language - C++ programming language - C++ glossary - C++ terms - C++ term - C++ programming glossary - C++ programming language glossary - Glossary of C++ - Glossary of C++ language - Glossary of C++ programming - Glossary of C++ programming language - Glossary of C++ programming language terms
C#
Clojure
- Clojure - Clojure language - Clojure programming language - Clojure glossary - Clojure terms - Clojure term - Clojure programming glossary - Clojure programming language glossary - Glossary of Clojure - Glossary of Clojure language - Glossary of Clojure programming - Glossary of Clojure programming language - Glossary of Clojure programming language terms
Dart
- Dart - Dart language - Dart programming language - Dart glossary - Dart terms - Dart term - Dart programming glossary - Dart programming language glossary - Glossary of Dart - Glossary of Dart language - Glossary of Dart programming - Glossary of Dart programming language - Glossary of Dart programming language terms
ECMAScript
Redirects to JavaScript
- ECMAScript - ECMAScript language - ECMAScript programming language - ECMAScript glossary - ECMAScript terms - ECMAScript term - ECMAScript programming glossary - ECMAScript programming language glossary - Glossary of ECMAScript - Glossary of ECMAScript language - Glossary ofECMAScript programming - Glossary of ECMAScript programming language - Glossary of ECMAScript programming language terms
Elixir
- Elixir - Elixir language - Elixir programming language - Elixir glossary - Elixir terms - Elixir term - Elixir programming glossary - Elixir programming language glossary - Glossary of Elixir - Glossary of Elixir language - Glossary of Elixir programming - Glossary of Elixir programming language - Glossary of Elixir programming language terms
- Elm - Elm language - Elm programming language - Elm glossary - Elm terms - Elm term - Elm programming glossary - Elm programming language glossary - Glossary of Elm - Glossary of Elm language - Glossary of Elm programming - Glossary of Elm programming language - Glossary of Elm programming language terms
Erlang
- Erlang - Erlang language - Erlang programming language - Erlang glossary - Erlang terms - Erlang term - Erlang programming glossary - Erlang programming language glossary - Glossary of Erlang - Glossary of Erlang language - Glossary of Erlang programming - Glossary of Erlang programming language - Glossary of Erlang programming language terms
F#
GoLang
Haskell
- Haskell - Haskell language - Haskell programming language - Haskell glossary - Haskell terms - Haskell term - Haskell programming glossary - Haskell programming language glossary - Glossary of Haskell - Glossary of Haskell language - Glossary of Haskell programming - Glossary of Haskell programming language - Glossary of Haskell programming language terms
Java
JavaScript
- JavaScript - JavaScript language - JavaScript programming language - JavaScript glossary - JavaScript terms - JavaScript term - JavaScript programming glossary - JavaScript programming language glossary - Glossary of JavaScript - Glossary of JavaScript language - Glossary of JavaScript programming - Glossary of JavaScript programming language - Glossary of JavaScript programming language terms
Kotlin
- Kotlin - Kotlin language - Kotlin programming language - Kotlin glossary - Kotlin terms - Kotlin term - Kotlin programming glossary - Kotlin programming language glossary - Glossary of Kotlin - Glossary of Kotlin language - Glossary of Kotlin programming - Glossary of Kotlin programming language - Glossary of Kotlin programming language terms
Lua
- Lua - Lua language - Lua programming language - Lua glossary - Lua terms - Lua term - Lua programming glossary - Lua programming language glossary - Glossary of Lua - Glossary of Lua language - Glossary of Lua programming - Glossary of Lua programming language - Glossary of Lua programming language terms
Machine code
- Machine code - Machine code language - Machine code programming language - Machine code glossary - Machine code terms - Machine code term - Machine code programming glossary - Machine code programming language glossary - Glossary of Machine code - Glossary of Machine code language - Glossary of Machine code programming - Glossary of Machine code programming language - Glossary of Machine code programming language terms
MATLAB
- MATLAB - MATLAB language - MATLAB programming language - MATLAB glossary - MATLAB terms - MATLAB term - MATLAB programming glossary - MATLAB programming language glossary - Glossary of MATLAB - Glossary of MATLAB language - Glossary of MATLAB programming - Glossary of MATLAB programming language - Glossary of MATLAB programming language terms
Node.js
Note: Node.js is not a programming language, but is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a web browser.
- Node.js - Node.js language - Node.js programming language - Node.js glossary - Node.js terms - Node.js term - Node.js programming glossary - Node.js programming language glossary - Glossary of Node.js - Glossary of Node.js language - Glossary of Node.js programming - Glossary of Node.js programming language - Glossary of Node.js programming language terms
Objective-C
- Objective-C - Objective-C language - Objective-C programming language - Objective-C glossary - Objective-C terms - Objective-C term - Objective-C programming glossary - Objective-C programming language glossary - Glossary of Objective-C - Glossary of Objective-C language - Glossary of Objective-C programming - Glossary of Objective-C programming language - Glossary of Objective-C programming language terms
PHP
- PHP - PHP language, PHP programming language - PHP glossary - PHP terms - PHP term - PHP programming glossary - PHP programming language glossary - Glossary of PHP - Glossary of PHP language - Glossary of PHP programming - Glossary of PHP programming language - Glossary of PHP programming language terms
Perl
- Perl - Perl language - Perl programming language - Perl glossary - Perl terms - Perl term - Perl programming glossary - Perl programming language glossary - Glossary of Perl - Glossary of Perl language - Glossary of Perl programming - Glossary of Perl programming language - Glossary of Perl programming language terms
PowerShell
R
Ruby
Rust
- Rust - Rust language - Rust programming language - Rust glossary - Rust terms - Rust term - Rust programming glossary - Rust programming language glossary - Glossary of Rust - Glossary of Rust language - Glossary of Rust programming - Glossary of Rust programming language - Glossary of Rust programming language terms
Scala
- Scala - Scala language - Scala programming language - Scala glossary - Scala terms - Scala term - Scala programming glossary - Scala programming language glossary - Glossary of Scala - Glossary of Scala language - Glossary of Scala programming - Glossary of Scala programming language - Glossary of Scala programming language terms
SQL
- SQL - SQL language - SQL programming language - SQL glossary - SQL terms - SQL term - SQL programming glossary - SQL programming language glossary - Glossary of SQL - Glossary of SQL language - Glossary of SQL programming - Glossary of SQL programming language - Glossary of SQL programming language terms
Swift
- Swift - Swift language - Swift programming language, Swift glossary - Swift terms - Swift term - Swift programming glossary - Swift programming language glossary - Glossary of Swift - Glossary of Swift language - Glossary of Swift programming - Glossary of Swift programming language - Glossary of Swift programming language terms
TypeScript
- TypeScript - TypeScript language - TypeScript programming language - TypeScript glossary - TypeScript terms - TypeScript term - TypeScript programming glossary - TypeScript programming language glossary - Glossary of TypeScript - Glossary of TypeScript language - Glossary of TypeScript programming - Glossary of TypeScript programming language - Glossary of TypeScript programming language terms
Visual Basic
- Visual Basic - Visual Basic language - Visual Basic programming language - Visual Basic glossary - Visual Basic terms - Visual Basic term - Visual Basic programming glossary - Visual Basic programming language glossary - Glossary of Visual Basic - Glossary of Visual Basic language - Glossary of Visual Basic programming - Glossary of Visual Basic programming language - Glossary of Visual Basic programming language terms
Visual Basic .NET
- Visual Basic .NET - Visual Basic .NET language - Visual Basic .NET programming language - Visual Basic .NET glossary - Visual Basic .NET terms - Visual Basic .NET term - Visual Basic .NET programming glossary - Visual Basic .NET programming language glossary - Glossary of Visual Basic .NET - Glossary of Visual Basic .NET language - Glossary of Visual Basic .NET programming - Glossary of Visual Basic .NET programming language - Glossary of Visual Basic .NET programming language terms
Sources
A method in object-oriented programming (OOP) is a procedure associated with a message and an object. An object is mostly made up of data and behavior, which form the interface that an object presents to the outside world. Data is represented as properties of the object and behavior as methods. For example, a
Window
object would have methods such as
open
and
close
, while its state (whether it is opened or closed) would be a property.
In class-based programming, methods are defined in a class, and objects are instances of a given class. One of the most important capabilities that a method provides is method overriding. The same name (e.g.,
area
) can be used for multiple different kinds of classes. This allows the sending objects to invoke behaviors and to delegate the implementation of those behaviors to the receiving object. A method in Java programming sets the behavior of a class object. For example, an object can send an
area
message to another object and the appropriate formula is invoked whether the receiving object is a
rectangle
,
circle
,
triangle
, etc.
Methods also provide the interface that other classes use to access and modify the data properties of an object. This is known as encapsulation. Encapsulation and overriding are the two primary distinguishing features between methods and procedure calls.<ref>
</ref>
Overriding and overloading
Method overriding and overloading are two of the most significant ways that a method differs from a conventional procedure or function call. Overriding refers to a subclass redefining the implementation of a method of its superclass. For example,
findArea
may be a method defined on a shape class. The various subclasses:
rectangle
,
circle
,
triangle
, etc. would each define the appropriate formula to calculate their area. The idea is to look at objects as “black boxes” so that changes to the internals of the object can be made with minimal impact on the other objects that use it. This is known as encapsulation and is meant to make code easier to maintain and re-use.
Method overloading, on the other hand, refers to differentiating the code used to handle a message based on the parameters of the method. If one views the receiving object as the first parameter in any method then overriding is just a special case of overloading where the selection is based only on the first argument.<ref>http://www.codeproject.com/Articles/16407/METHOD-Overload-Vs-Overriding</ref> The following simple Java example illustrates the difference:<ref>
</ref>
<syntaxhighlight lang=“java”> public class class1 {
int f(int x) { return x+3; }
}
public class class2 extends class1 {
@Override int f(int x) { // overriding return x*x; } int f(int x, int y) { // overloading return x*y; }
} </syntaxhighlight>
Accessor, mutator and manager methods
Accessor methods are used to read 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.<ref>
</ref>
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:
<syntaxhighlight lang=“java”> public class Main {
String name; int roll; Main(String _name, int _roll) { //constructor method this.name = _name; this.roll = _roll; }
} </syntaxhighlight>
Destructors
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.
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
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 computer languages.<ref>
</ref>
Example
The following Java code shows an abstract class that needs to be extended:
<syntaxhighlight lang=“java”> abstract class Shape {
abstract int area(int h, int w); // abstract method signature} </syntaxhighlight> The following subclass extends the main class:
<syntaxhighlight lang=“java”> public class Rectangle extends Shape {
@Override int area(int h, int w) { return h * w; }} </syntaxhighlight>
Class methods
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
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
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:
Math.max(double a, double b)This static method has no owning object and does not run on an instance. It receives all information from its arguments.<ref>
</ref>
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
Operator methods define or redefine operator symbols and define the operations to be performed with the symbol and the associated method parameters. C++ Example: <syntaxhighlight lang=“cpp”> class data { public:
string name; int roll; bool operator < (const data& p) const { return roll < p.roll; } bool operator == (const data& p) const { return (name == p.name) and (roll == p.roll); }
}; </syntaxhighlight>
Member functions in C++
Some procedural languages were extended with object-oriented capabilities to leverage the large skill sets and legacy code for those languages but still provide the benefits of object-oriented development. Perhaps the most well-known example is C++, an object-oriented extension of the C programming language. Due to the design requirements to add the object-oriented paradigm on to an existing procedural language, message passing in C++ has some unique capabilities and terminologies. For example, in C++ a method is known as a member function. C++ also has the concept of virtual functions which are member functions that can be overridden in derived classes and allow for dynamic dispatch.
Virtual functions
Virtual functions are the means by which a C++ class can achieve polymorphic behavior. Non-virtual member functions, or regular methods, are those that do not participate in polymorphism.
C++ Example: <syntaxhighlight lang=“cpp”>
- include <iostream>
- include <memory>
class Super { public:
virtual void iAm() { std::cout << "I'm the super class!\n"; }
};
class Sub : public Super { public:
void iAm() { std::cout << "I'm the subclass!\n"; }
};
int main() {
std::unique_ptr<Super> inst1(new Super()); std::unique_ptr<Super> inst2(new Sub());
inst1->iAm(); // calls Super::iAm() inst2->iAm(); // calls Sub::iAm()
} </syntaxhighlight>
See also
- Subroutine, also called subprogram, routine, procedure, or function
- Snippet from Wikipedia: Method (computer programming)
A method in object-oriented programming (OOP) is a procedure associated with an object, and generally also a message. An object consists of state data and behavior; these compose an interface, which specifies how the object may be used. A method is a behavior of an object parametrized by a user.
Data is represented as properties of the object, and behaviors are represented as methods. For example, a
Window
object could have methods such asopen
andclose
, while its state (whether it is open or closed at any given point in time) would be a property.In class-based programming, methods are defined within a class, and objects are instances of a given class. One of the most important capabilities that a method provides is method overriding - the same name (e.g.,
area
) can be used for multiple different kinds of classes. This allows the sending objects to invoke behaviors and to delegate the implementation of those behaviors to the receiving object. A method in Java programming sets the behavior of a class object. For example, an object can send anarea
message to another object and the appropriate formula is invoked whether the receiving object is arectangle
,circle
,triangle
, etc.Methods also provide the interface that other classes use to access and modify the properties of an object; this is known as encapsulation. Encapsulation and overriding are the two primary distinguishing features between methods and procedure calls.