Questions tagged [methods]

A method is a block of code that performs a task and is associated with a class or an object. It is related to the non-object-oriented concepts of functions and procedures.

A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments.

In object-oriented programming, a method is a subroutine (or procedure) 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.

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

Common features

They have few things in common:

  1. They may take some parameters / arguments for their processing.
  2. They may have a return-value, that returns some value to the calling method.
  3. The concept of and is also associated with methods.

References

Also see:

30291 questions
141
votes
9 answers

Passing a method as a parameter in Ruby

I am trying to mess around a little bit with Ruby. Therefor I try to implement the algorithms (given in Python) from the book "Programming Collective Intelligence" Ruby. In chapter 8 the author passes a method a as parameter. This seems to work in…
Christian Stade-Schuldt
  • 4,671
  • 7
  • 35
  • 30
141
votes
8 answers

unbound method f() must be called with fibo_ instance as first argument (got classobj instance instead)

In Python, I'm trying to run a method in a class and I get an error: Traceback (most recent call last): File "C:\Users\domenico\Desktop\py\main.py", line 8, in fibo.f() TypeError: unbound method f() must be called with fibo instance…
DomeWTF
  • 2,342
  • 4
  • 33
  • 46
140
votes
13 answers

Optional Methods in Java Interface

From my understanding if you implement an interface in java, the methods specified in that interface have to be used by the sub classes implementing the said interface. I've noticed that in some interfaces such as the Collection interface there are…
mjsey
  • 1,938
  • 5
  • 18
  • 28
139
votes
13 answers

How do I call a dynamically-named method in Javascript?

I am working on dynamically creating some JavaScript that will be inserted into a web page as it's being constructed. The JavaScript will be used to populate a listbox based on the selection in another listbox. When the selection of one listbox is…
Chris B
  • 5,311
  • 11
  • 45
  • 57
136
votes
5 answers

Usage of @see in JavaDoc?

When do I use @see when dealing with JavaDocs? What is its usage? For example if MethodA calls MethodB then do I have to put @see in MethodB's javadoc and reference MethodA because that is what called it, or do I have to put a reference to MethodB…
Jeff
  • 2,017
  • 3
  • 15
  • 8
133
votes
5 answers

Python Method overriding, does signature matter?

Lets say I have class Super(): def method1(): pass class Sub(Super): def method1(param1, param2, param3): stuff Is this correct? Will calls to method1 always go to the sub class? My plan is to have 2 sub classes each override method1…
asdasasdsa
  • 1,331
  • 2
  • 9
  • 3
132
votes
6 answers

In Python, when should I use a function instead of a method?

The Zen of Python states that there should only be one way to do things- yet frequently I run into the problem of deciding when to use a function versus when to use a method. Let's take a trivial example- a ChessBoard object. Let's say we need some…
Ceasar
  • 22,185
  • 15
  • 64
  • 83
131
votes
7 answers

Why does this C++ snippet compile (non-void function does not return a value)

I found this in one of my libraries this morning: static tvec4 Min(const tvec4& a, const tvec4& b, tvec4& out) { tvec3::Min(a,b,out); out.w = min(a.w,b.w); } I'd expect a compiler error because this method doesn't return anything, and the…
3Dave
  • 28,657
  • 18
  • 88
  • 151
129
votes
12 answers

Defining an abstract class without any abstract methods

Can I define an abstract class without adding an abstract method?
VisaMasterCard
  • 1,666
  • 4
  • 18
  • 22
125
votes
5 answers

Why not abstract fields?

Why can't Java classes have abstract fields like they can with abstract methods? For example: I have two classes that extend the same abstract base class. These two classes each have a method that is identical except for a String constant, which…
Paul Reiners
  • 8,576
  • 33
  • 117
  • 202
124
votes
9 answers

new keyword in method signature

While performing a refactoring, I ended up creating a method like the example below. The datatype has been changed for simplicity's sake. I previous had an assignment statement like this: MyObject myVar = new MyObject(); It was refactored to this…
p.campbell
  • 98,673
  • 67
  • 256
  • 322
122
votes
10 answers

How to check whether a method exists in Python?

In the function __getattr__(), if a referred variable is not found then it gives an error. How can I check to see if a variable or method exists as part of an object? import string import logging class Dynamo: def __init__(self,x): print "In…
Rajeev
  • 44,985
  • 76
  • 186
  • 285
120
votes
16 answers

What's wrong with using $_REQUEST[]?

I've seen a number of posts on here saying not to use the $_REQUEST variable. I usually don't, but sometimes it's convenient. What's wrong with it?
sprugman
  • 19,351
  • 35
  • 110
  • 163
119
votes
5 answers

Javascript dynamically invoke object method from string

Can I dynamically call an object method having the method name as a string? I would imagine it like this: var FooClass = function() { this.smile = function() {}; } var method = "smile"; var foo = new FooClass(); // I want to run smile on the…
Mikulas Dite
  • 7,790
  • 9
  • 59
  • 99
119
votes
6 answers

Why are C# interface methods not declared abstract or virtual?

C# methods in interfaces are declared without using the virtual keyword, and overridden in the derived class without using the override keyword. Is there a reason for this? I assume that it is just a language convenience, and obviously the CLR…
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501