Actions

Method

Revision as of 18:50, 17 February 2021 by User (talk | contribs) (Created page with "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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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]

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]

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

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:


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 programming languages.[6]

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

  1. Definition of a Method Techterms
  2. Explaining the Meaning of Method Techopedia