Method (computer science)

A method (or a member function) in object-oriented programming (OOP) is a subroutine associated with an object of a class (computer science) that forms its interface through which the outside members of the class (other objects) can access its private members (mainly the encapsulated data). Methods ("member functions") are similar to functions, they belong to classes or objects and usually expresses the verbs of the objects/class. For example, an object of type Window usually would have methods open and close which do corresponding operations to the object they belong.

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.


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

abstract class Main {
    abstract int rectangle(int h, int w); // abstract method signature

The following subclass extends the main class:

public class Main2 extends Main {
    int rectangle(int h, int w)
        return h * w;

Accessor, mutator and manager methods

Accessor methods (also called getters) are used to read values of private data members of an object which are directly inaccessible to non member methods. Mutator methods (also called setters) are used to modify values of private data members of an object. Manager methods are used to initialize and destroy objects of a class, e.g. constructors and destructors.

An 'accessor' method is a method that is usually small, simple and provides the sole means for accessing (retrieving) the state of an object 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). Exact meaning varies depending on the programming languages.

In some languages (e.g. Smalltalk, Python, Ruby, Objective-C, Swift), class methods are methods that are called on a class object. This is virtually identical to instance methods, except that in class methods this refers to the class object, not the instance object. In these languages, class methods are resolved dynamically (at runtime), just like instance methods. Class methods may be implemented as class objects being instances of a Class class (hence called a metaclass), and the class method on the class (instance of metaclass) being an instance method on the metaclass, in which case class methods are just a form of instance methods.

In other languages, notably C++ and Java, class methods are synonymous with static methods (see below), which are resolved at compile-time with a known class name. this cannot be used in static methods, either to refer to the instance or the class.

Python is unusual in having both class methods in the object-oriented sense – methods called on a class object, referred to as cls instead of self for class instance objects – which are created with the classmethod decorator; and functions that are defined in a class – and can be overridden in derived classes – but have no this, and cannot refer to either the instance object or the class object. These latter are called "static methods", by analogy with C++ and Java, and are created with the staticmethod decorator, but are resolved dynamically (at run time). Further, Python resolves methods on the class if no matches are found on an object, thus it may resolve a method call on an object to a class method or static method, based on the type of the object.

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.

Overloaded methods

Overloaded methods are those that appear to have the same name, but that have different formal parameter types (or result value type, if the language supports overloading on result type). [1]

Overridden methods

Overridden methods are those that are redefined in a subclass and hide methods of a superclass. The new method can use the previous definition through a special mechanism, for example, the super keyword in Smalltalk and Java. Some people confuse overriding with overloaded methods, but they are really quite different; the main difference is that the choice of method from a set of overriding methods is made according to the class of the receiver of the method request, whereas the name of an overloaded method is constructed according to the static types of the arguments to the method request. Another difference is that an overriding method must be declared in a subclass of the class that declared the overridden method, while several overloadings of a method name can be declared in the same class.[2] Look at the following example from Java:

public class class1
        int f(int x)
                return x+3;

public class class2 extends class1
        int f(int x) // overriding
                return x*x;
        int f(int x, int y) // overloading
                return x*y;

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. See the following example in Java:

public class Main
        String name;
        int roll;
        Main(String _name, int _roll) { //constructor method
                (this).name = _name;
                (this).roll = _roll;


A destructor is a class 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. (In some languages , 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). Destruction can be implemented so as to perform cleanup 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.

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:

class data
        string name;
        int roll;
        bool operator < (const data& p) const
                return roll < p.roll;
        bool operator == (const data& p) const
                return (name == and (roll == p.roll);

Some programming languages don't support operator methods as they might create confusion. For details read: Operator overloading.

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.

In statically typed languages such as Java, 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.[4]

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

C++ Example:


class Super
        virtual void iAm() { std::cout << "I'm the super class!\n"; }

class Sub : public Super
        void iAm() { std::cout << "I'm the subclass!\n"; }

int main()
        std::unique_ptr inst1(new Super());
        std::unique_ptr inst2(new Sub());

        inst1->iAm(); // calls Super::iAm()
        inst2->iAm(); // calls Sub::iAm()


  1. ^ John Suzuki (2000-02-18). "What is an overloaded method?". j Guru. 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. 
  2. ^
  3. ^ 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. 
  4. ^
  5. ^ 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. 
  • Sengupta, Probal (1 August 2004). Object-Oriented Programming: Fundamentals And Applications. PHI Learning Pvt. Ltd. ISBN . 
  • Svenk, Goran (2003). Object-oriented Programming: Using C++ for Engineering and Technology. Cengage Learning. ISBN . 
  • Balagurusamy (2013). Object Oriented Programming with C++. Tata McGraw-Hill Education. ISBN . 
  • Kirch-Prinz, Ulla; Prinz, Peter (2002). A Complete Guide to Programming in C++. Jones & Bartlett Learning. ISBN . 
  • Conger, David (2006). Creating Games in C++: A Step-by-step Guide. New Riders. ISBN . 
  • Skinner, M. T. (1992). The Advanced C++ Book. Silicon Press. ISBN . 
  • Love (1 September 2005). Linux Kernel Development. Pearson Education. ISBN . 

See also

This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.