Actions

Difference between revisions of "Method"

(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
A '''Method''' is a subroutine attached to a specific class defined in the [[Source Code|source code]] of a [[Computer Program|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 (OOP)|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|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.<ref>Definition of a Method [https://techterms.com/definition/method Techterms]</ref>
+
===What is a Method===
 +
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|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.<ref>[https://techterms.com/definition/method Definition of a Method]</ref>
  
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:
+
Methods are usually comprehensive approaches, structuring the whole process of system development. Therefore, a method typically has a holistic
 +
scope and covers all tasks and activities, necessary to plan, design, and implement a system. Although there is much research on the topics of methods and method engineering (e.g. Ralyté et al. 2007), a generally accepted definition of ‘method’ including its components is still missing. As a result, ‘process model’, ‘lifecycle model’ as well as ‘technique’ are often used synonymously or interchangeably.
 +
 
 +
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.
 
*Play: Begin playing the movie clip.
 
*Pause: Pause the movie clip.
 
*Pause: Pause the movie clip.
*Stop: Stop playing the movie clip.<ref>Explaining the Meaning of Method [https://www.techopedia.com/definition/3231/method Techopedia]</ref>
+
*Stop: Stop playing the movie clip.<ref>[https://www.techopedia.com/definition/3231/method Explaining the Meaning of Method]</ref>
 +
 
 +
In IS/CS research, the notion of the term ‘method’ is mainly related to system development and similar disciplines. In general ‘method’ is being used as a quite generic term – coming from Greece “méthodos” (way/procedure). In the context of IT Management, 'methods' are usually comprehensive approaches, structuring the whole process of system development. Therefore, a method typically has a holistic scope and covers all tasks and activities, necessary to plan, design, and implement a system. Although there is much research on the topics of methods and method engineering (e.g. Ralyté et al. 2007), a generally accepted definition of ‘method’ including its components is still missing. As a result, ‘process model’, ‘lifecycle model’ as well as ‘technique’ are often used synonymously or interchangeably.
 +
 
  
===Method Types<ref>Types of Methods [https://en.wikipedia.org/wiki/Method_(computer_programming) Wikipedia]</ref>===
+
===Method Types<ref>[https://en.wikipedia.org/wiki/Method_(computer_programming) Types of Methods]</ref>===
 
'''Accessor, Mutator and Manager Methods'''<br />
 
'''Accessor, Mutator and Manager Methods'''<br />
 
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.
 
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.
+
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 (DB)|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''<br />
 
''Constructors''<br />
 
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 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:
  
[[File:constructors.png|300px|constructors]]<br />
+
[[File:constructors.png|600px|constructors]]<br />
  
 
'''Destructors'''<br />
 
'''Destructors'''<br />
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.
+
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 for object destruction.
  
 
'''Finalizers'''<br />
 
'''Finalizers'''<br />
Line 24: Line 31:
  
 
'''Abstract methods'''<br />
 
'''Abstract methods'''<br />
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.[6]
+
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 Language|programming languages]].
  
 
Example<br />
 
Example<br />
 
The following Java code shows an abstract class that needs to be extended:<br />
 
The following Java code shows an abstract class that needs to be extended:<br />
  
[[File:abstract class needs to be extended.png|300px|abstract class needs to be extended]]<br />
+
[[File:abstract class needs to be extended.png|600px|abstract class needs to be extended]]<br />
  
 
The following subclass extends the main class:<br />
 
The following subclass extends the main class:<br />
  
[[File:sub class extends the main class.png|300px|sub class extends the main class]]<br />
+
[[File:sub class extends the main class.png|600px|sub class extends the main class]]<br />
  
 
'''Reabstraction'''<br />
 
'''Reabstraction'''<br />
Line 43: Line 50:
 
In C#, a virtual method can be overridden with an abstract method. (This also applies to Java, where all non-private methods are virtual.)
 
In C#, a virtual method can be overridden with an abstract method. (This also applies to Java, where all non-private methods are virtual.)
  
[[File:virtual method overridden by abstract method.png|300px|virtual method overridden by abstract method]]<br />
+
[[File:virtual method overridden by abstract method.png|600px|virtual method overridden by abstract method]]<br />
  
 
Interfaces' default methods can also be reabstracted, requiring subclasses to implement them. (This also applies to Java.)
 
Interfaces' default methods can also be reabstracted, requiring subclasses to implement them. (This also applies to Java.)
  
[[File:interfaces default methods.png|300px|interfaces default methods]]<br />
+
[[File:interfaces default methods.png|600px|interfaces default methods]]<br />
  
 
'''Class methods'''<br />
 
'''Class methods'''<br />
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.
+
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, a defined 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'''<br />
 
'''Special methods'''<br />
Line 56: Line 63:
  
 
'''Static methods'''<br />
 
'''Static methods'''<br />
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.
+
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 [[Product|products]].
  
 
In Java, a commonly used static method is:
 
In Java, a commonly used static method is:
  
[[File:static method.png|300px|static method]]<br />
+
[[File:static method.png|600px|static method]]<br />
  
 
This static method has no owning object and does not run on an instance. It receives all information from its arguments.
 
This static method has no owning object and does not run on an instance. It receives all information from its arguments.
Line 72: Line 79:
 
Operator methods define or redefine operator symbols and define the operations to be performed with the symbol and the associated method parameters. C++ example:
 
Operator methods define or redefine operator symbols and define the operations to be performed with the symbol and the associated method parameters. C++ example:
  
[[File:operator method.png|300px|operator method]]<br />
+
[[File:operator method.png|600px|operator method]]<br />
 +
 
 +
 
 +
===See Also===
 +
[[Process]]<br />
 +
[[Procedure]]<br />
 +
[[Methodology]]<br />
 +
[[Model]]<br />
 +
[[Framework]]<br />
 +
[[Best Practice]]
  
  
 
===References===
 
===References===
 
<references/>
 
<references/>
 +
 +
__NOTOC__

Revision as of 19:48, 19 October 2022

What is a Method

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.[1]

Methods are usually comprehensive approaches, structuring the whole process of system development. Therefore, a method typically has a holistic scope and covers all tasks and activities, necessary to plan, design, and implement a system. Although there is much research on the topics of methods and method engineering (e.g. Ralyté et al. 2007), a generally accepted definition of ‘method’ including its components is still missing. As a result, ‘process model’, ‘lifecycle model’ as well as ‘technique’ are often used synonymously or interchangeably.

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.[2]

In IS/CS research, the notion of the term ‘method’ is mainly related to system development and similar disciplines. In general ‘method’ is being used as a quite generic term – coming from Greece “méthodos” (way/procedure). In the context of IT Management, 'methods' are usually comprehensive approaches, structuring the whole process of system development. Therefore, a method typically has a holistic scope and covers all tasks and activities, necessary to plan, design, and implement a system. Although there is much research on the topics of methods and method engineering (e.g. Ralyté et al. 2007), a generally accepted definition of ‘method’ including its components is still missing. As a result, ‘process model’, ‘lifecycle model’ as well as ‘technique’ are often used synonymously or interchangeably.


Method Types[3]

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.

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:

constructors

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 for 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 programming languages.

Example
The following Java code shows an abstract class that needs to be extended:

abstract class needs to be extended

The following subclass extends the main class:

sub class extends the main class

Reabstraction
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.

Example
In C#, a virtual method can be overridden with an abstract method. (This also applies to Java, where all non-private methods are virtual.)

virtual method overridden by abstract method

Interfaces' default methods can also be reabstracted, requiring subclasses to implement them. (This also applies to Java.)

interfaces default methods

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, a defined 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:

static method

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
Operator methods define or redefine operator symbols and define the operations to be performed with the symbol and the associated method parameters. C++ example:

operator method


See Also

Process
Procedure
Methodology
Model
Framework
Best Practice


References