Method (computer programming)

Method (computer programming)

In object-oriented programming, a method is a subroutine (or procedure or function) associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time. Methods have the special property that at runtime, they have access to data stored in an instance of the class (or class instance or class object or object) they are associated with and are thereby able to control the state of the instance. [1] The association between class and method is called binding. A method associated with a class is said to be bound to the class. Methods can be bound to a class at compile time (static binding) or to an object at runtime (dynamic binding).[2]


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

Accessor and mutator methods

An 'bikhari' method is a method that is usually small, simple and provides the sole means for the state of an object to be accessed (retrieved) from other parts of a program.

Although this introduces a new dependency, as stated above, use of methods is preferred, in the object-oriented paradigm, to directly accessing state data - because those methods provide an abstraction layer. 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 (say, a database fetch), without the dependent code needing to be changed (However, this often claimed advantage is not unique to object oriented programming, and was earlier implemented - when desirable in critical systems - through conventional modular programming with optional run-time, system-wide locking mechanisms, in the imperative/procedural paradigms)
To compare the value of two data items, two accessor-method calls are normally required before a comparison can take place between the retrieved primitive data type values. Comparator methods are required to compare entire objects for equality. This contrasts with a direct comparison in non-OOP paradigms.

An update, modifier, or mutator method, is an accessor method that changes the state of an object. Objects that provide such methods are considered mutable objects.

Class methods

Class methods are methods that are called on a class (compare this to class instance methods, or object methods). Its meaning may vary depending on the programming language:

  • In some languages (e.g. C++, Java), class methods are synonymous with static methods (see section below), which are called with a known class name at compile-time. this cannot be used in static methods.
  • In some other languages (e.g. Smalltalk, Ruby, Objective-C), class methods are methods that are called on a class object, which can be computed at runtime, there being no difference between calling a method on a regular object or a class object; thus both instance and class methods are resolved dynamically, and there are no "static" methods. Notably, in these class methods, this refers to the class object.
  • Some languages have both. For example, in Python, one can create class methods and static methods using the classmethod and staticmethod decorators, respectively. The former has access to this, while the latter does not.

Conversion operator methods

A conversion operator provides a means for the compiler to implicitly (performed by the compiler automatically when appropriate) provide an object of a type other than the type of the class object.

Operator methods

Operator methods define or redefine operator symbols and define the operations to be performed with the symbol and the associated method parameters.

Overloaded methods

Overloaded methods are those with the same name but different formal parameters or return value type, if the language supports overloading on return type.[4]

Overridden methods

Overridden methods are those that are redefined in a subclass and hide methods of a superclass.[5]

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. The syntax for definition and calling (i.e., when a special method can be called) of special methods varies amongst programming languages.


A constructor, is a class method that is called automatically at the beginning of an object's lifetime to initialize the object, a process called construction (or instantiation). Initialization may include acquisition of resources. A language may provide a means to control whether a constructor can be called implicitly (by the compiler) or only explicitly (by the programmer). Constructors may have parameters but usually do not return values in most languages.


A destructor is a class method that is called automatically at the end of an object's lifetime, a process called destruction. Destructors in most languages do not allow destructor method arguments nor return values. (In some languages (ref required), a destructor can return a value which can then be used to obtain a public representation (transfer encoding) of an instance of a class and simultaneously destroy the copy of the instance stored in current thread's memory.) Destructors can be implemented such as to perform clean up chores and other tasks at object destruction.

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.

Static methods

Static methods neither require an instance of the class nor can they implicitly access the data (or this, self, Me, etc.) of such an instance. A static method is distinguished in some programming languages with the static keyword placed somewhere in the method's signature. Static methods are called "static" because they are resolved statically (i.e. 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. Therefore, static methods cannot be overridden.

Virtual methods

Virtual methods are the means by which a class object can achieve polymorphic behavior. Non-virtual methods, or regular methods, are those which do not participate in polymorphism. [6]


  1. ^ Mark Slagell (2008). "Methods". Ruby User's Guide. Retrieved 2011-08-12. "What is a method? In OO programming, we don't think of operating on data directly from outside an object; rather, objects have some understanding of how to operate on themselves (when asked nicely to do so). You might say we pass messages to an object, and those messages will generally elicit some kind of an action or meaningful reply. This ought to happen without our necessarily knowing or caring how the object really works inside. The tasks we are allowed to ask an object to perform (or equivalently, the messages it understands) are that object's methods." 
  2. ^ "OOPS Interview Questions: 17. What is Binding?". TOLMOL Beta. Retrieved 2011-08-12. "Binding denotes association of a name with a class." 
  3. ^ "What Is an Abstract Method?". wiseGEEK. 
  4. ^ John Zukowski (2000-02-18). "What is an overloaded method?". jGuru. Retrieved 2011-08-12. "Overloaded methods are multiple methods in the same class that share the same name but have different parameter lists. Overloaded methods cannot have the same parameter lists with different return types." 
  5. ^ Anandvijayakumar. "What is an overridden method and overriding a method?". Retrieved 2011-08-12. "Assuming class A has a method named getXXX() and class B is a sub class of class A. Now, if we write a method with the same name getXXX() in class B, with exactly the same signature as class A, it is called overriding a method." 
  6. ^ Alexis Angelidis. "What is a virtual method?". Online C++ FAQ/Tutorial and Advanced Questions. Retrieved 2011-08-12. "A virtual method in a parent allows children to have a different implementation for it. A pure virtual method in a parent forces children to have an implementation for it (interface in Java). A class with a pure virtual method is called virtual." 

See also

Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • Computer programming — Programming redirects here. For other uses, see Programming (disambiguation). Software development process Activities and steps …   Wikipedia

  • computer programming language — Introduction       any of various languages for expressing a set of detailed instructions for a digital computer. Such instructions can be executed directly when they are in the computer manufacturer specific numerical form known as machine… …   Universalium

  • Class (computer programming) — In object oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable …   Wikipedia

  • Skeleton (computer programming) — Skeleton programming is a style of computer programming based on simple high level program structures and so called dummy code. Program skeletons resemble pseudocode, but allow parsing, compilation and testing of the code. Dummy code is inserted… …   Wikipedia

  • Comment (computer programming) — For comments in Wikipedia markup, see Help:Wiki markup#Character formatting and WP:COMMENT. An illustration of Java source code with prologue comments indicated in red and inline comments in green. Program code is in blue …   Wikipedia

  • Ch (computer programming) — In computing, Ch (  /ˌsiːˈ …   Wikipedia

  • Destructor (computer programming) — In object oriented programming, a destructor (sometimes shortened to dtor) is a method which is automatically invoked when the object is destroyed. Its main purpose is to clean up and to free the resources (which includes closing database… …   Wikipedia

  • Method (computer science) — In object oriented programming, the term method refers to a subroutine that is exclusively associated either with a class (called class methods, static methods, or factory methods) or with an object (called instance methods). Like a procedure in… …   Wikipedia

  • Reflection (computer programming) — Programming paradigms Agent oriented Automata based Component based Flow based Pipelined Concatenative Concurrent computi …   Wikipedia

  • Method — may refer to: Scientific method, a series of steps taken to acquire knowledge Method (computer programming), a piece of code associated with a class or object to perform a task Method (music), a kind of textbook to help students learning to play… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”