Method
- In Turkish
- Metot
- Pronunciation
- METH-ud
In short
A method is a function that belongs to an object or class and is called on it, so it can read and change that object's data, as in user.rename("Ada").
What is a method in programming?
A method is defined inside a class, or attached to an object, and is called with a dot: cart.addItem(book), "hello".toUpperCase(). Inside the method, the object it was called on is available as this in JavaScript, Java and C#, or as self in Python and Ruby, so the method can use and update that object's fields.
Instance methods work on one particular object, such as account.deposit(50). Static methods, also called class methods, belong to the class itself and don't need an object, like Math.max(3, 7) or a factory such as User.fromJson(data). Many languages also have getters and setters, methods that look like plain property access.
Methods are how objects expose behavior while hiding their internal details. Callers ask the object to do something, order.cancel(), instead of changing its fields directly; the method can check rules, update related data and keep the object valid. Subclasses can override a method to change what it does, which is the basis of polymorphism.
A common misconception is that methods and functions are completely different things. A method is a function; the difference is only that it is tied to an object or class and receives that object implicitly. In JavaScript, a method taken off its object and called alone can even lose its this, a frequent source of bugs.
Key takeaways
- A method is a function that belongs to an object or class.
- It reaches its object through this (or self in Python and Ruby).
- Instance methods work on one object; static methods belong to the class.
- Methods let objects expose behavior and protect their internal state.
- Subclasses can override methods, which enables polymorphism.
Example
class Account:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amount): # instance method: works on one account
if amount <= 0:
raise ValueError("Amount must be positive")
self.balance += amount
@staticmethod
def fee_for(amount): # static method: needs no account
return round(amount * 0.01, 2)
acct = Account("Ada")
acct.deposit(100)
print(acct.balance, Account.fee_for(100))Readers ask
What is the difference between a method and a function?
A function stands on its own; a method is a function attached to an object or class and called through it. The method also receives that object, as this or self, so it can work with its data.
What is a static method?
A method that belongs to the class rather than to any one object. It is called on the class, like Math.round(2.5), and cannot use instance fields unless an object is passed in.
What is method overriding?
Defining a method in a subclass with the same name as one in its parent class, so objects of the subclass use the new version. It lets different types respond to the same call in their own way.
See also
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- ObjectProgramming Fundamentals, p. 39An object is a bundle of related data and behavior: values in named fields, often called properties, and functions that work on them, called methods.
- ClassProgramming Fundamentals, p. 9A class is a blueprint in object-oriented programming that defines the data and behavior shared by a group of objects, which are created from it as instances.
- PolymorphismProgramming Fundamentals, p. 45Polymorphism is the ability of code to work with values of different types through one shared interface, with each type supplying its own behavior.
- EncapsulationProgramming Fundamentals, p. 17Encapsulation is the practice of bundling data with the methods that use it and hiding internal details, so outside code works only through a public interface.
- OOPProgramming Fundamentals, p. 40OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
Spotted a mistake or something missing on this page?Suggest an edit