Method (computer programming)
A method in
Data is represented as properties of the object, and behaviors are represented as methods. For example, a Window
object could have methods such as open
and close
, while its state (whether it is open or closed at any given point in time) would be a property.
In
area
) can be used for multiple different kinds of classes. This allows the sending objects to invoke behaviors and to delegate the implementation of those behaviors to the receiving object. A method in Javaarea
message to another object and the appropriate formula is invoked whether the receiving object is a rectangle
, circle
, triangle
, etc.
Methods also provide the interface that other classes use to access and modify the properties of an object; this is known as encapsulation. Encapsulation and overriding are the two primary distinguishing features between methods and procedure calls.[1]
Overriding and overloading
Method overriding and overloading are two of the most significant ways that a method differs from a conventional procedure or function call. Overriding refers to a subclass redefining the implementation of a method of its superclass. For example, findArea
may be a method defined on a shape class,[2] triangle
, etc. would each define the appropriate formula to calculate their area. The idea is to look at objects as "black boxes" so that changes to the internals of the object can be made with minimal impact on the other objects that use it. This is known as encapsulation and is meant to make code easier to maintain and re-use.
Method overloading, on the other hand, refers to differentiating the code used to handle a message based on the parameters of the method. If one views the receiving object as the first parameter in any method then overriding is just a special case of overloading where the selection is based only on the first argument. The following simple Java example illustrates the difference:
Accessor, mutator and manager methods
These methods provide an
Constructors
A
public class Main {
String _name;
int _roll;
Main(String name, int roll) { // constructor method
this._name = name;
this._roll = roll;
}
}
Destructor
A
Finalizers
In garbage-collected languages, such as Java,[4]: 26, 29 C#,[5]: 208–209 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
Example
The following Java code shows an abstract class that needs to be extended:
abstract class Shape {
abstract int area(int h, int w); // abstract method signature
}
The following subclass extends the main class:
public class Rectangle extends Shape {
@Override
int area(int h, int w) {
return h * w;
}
}
Reabstraction
If a subclass provides an implementation for an abstract method, another subclass can make it abstract again. This is called reabstraction.
In practice, this is rarely used.
Example
In C#, a virtual method can be overridden with an abstract method. (This also applies to Java, where all non-private methods are virtual.)
class IA
{
public virtual void M() { }
}
abstract class IB : IA
{
public override abstract void M(); // allowed
}
Interfaces' default methods can also be reabstracted, requiring subclasses to implement them. (This also applies to Java.)
interface IA
{
void M() { }
}
interface IB : IA
{
abstract void IA.M();
}
class C : IB { } // error: class 'C' does not implement 'IA.M'.
Class methods
Class methods are methods that are called on a
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.
Static methods
Static methods are meant to be relevant to all the instances of a class rather than to any specific instance. They are similar to static variables in that sense. An example would be a static method to sum the values of all the variables of every instance of a class. For example, if there were a Product
class it might have a static method to compute the average price of all products.
A static method can be invoked even if no instances of the class exist yet. Static methods are called "static" because they are resolved 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.
Examples
In Java
In Java, a commonly used static method is:
Math.max(double a, double b)
This static method has no owning object and does not run on an instance. It receives all information from its arguments.[2]
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:
#include <string>
class Data {
public:
bool operator<(const Data& data) const { return roll_ < data.roll_; }
bool operator==(const Data& data) const {
return name_ == data.name_ && roll_ == data.roll_;
}
private:
std::string name_;
int roll_;
};
Member functions in C++
Some procedural languages were extended with object-oriented capabilities to leverage the large skill sets and legacy code for those languages but still provide the benefits of object-oriented development. Perhaps the most well-known example is
Virtual functions
Virtual functions are the means by which a C++ class can achieve polymorphic behavior. Non-virtual member functions, or regular methods, are those that do not participate in polymorphism.
C++ Example:
#include <iostream>
#include <memory>
class Super {
public:
virtual ~Super() = default;
virtual void IAm() { std::cout << "I'm the super class!\n"; }
};
class Sub : public Super {
public:
void IAm() override { std::cout << "I'm the subclass!\n"; }
};
int main() {
std::unique_ptr<Super> inst1 = std::make_unique<Super>();
std::unique_ptr<Super> inst2 = std::make_unique<Sub>();
inst1->IAm(); // Calls |Super::IAm|.
inst2->IAm(); // Calls |Sub::IAm|.
}
See also
- Property (programming)
- Remote method invocation
- Subroutine, also called subprogram, routine, procedure or function
Notes
- ^ "What is an Object?". oracle.com. Oracle Corporation. Retrieved 13 December 2013.
- ^ ISBN 978-0-13-235088-4.
- ISBN 0-13-629049-3.
- ISBN 978-0134685991.
- ISBN 978-1-098-12195-2.
- ^ "Abstract Methods and Classes". oracle.com. Oracle Java Documentation. Retrieved 11 December 2014.
References
- JANA, DEBASISH (1 January 2005). C++ and Object-oriented Programming Paradigm. PHI Learning Pvt. Ltd. ISBN 978-81-203-2871-6.
- Sengupta, Probal (1 August 2004). Object-Oriented Programming: Fundamentals And Applications. PHI Learning Pvt. Ltd. ISBN 978-81-203-1258-6.
- Svenk, Goran (2003). Object-oriented Programming: Using C++ for Engineering and Technology. Cengage Learning. ISBN 0-7668-3894-3.
- Balagurusamy (2013). Object Oriented Programming with C++. Tata McGraw-Hill Education. ISBN 978-1-259-02993-6.
- Kirch-Prinz, Ulla; Prinz, Peter (2002). A Complete Guide to Programming in C++. Jones & Bartlett Learning. ISBN 978-0-7637-1817-6.
- Conger, David (2006). Creating Games in C++: A Step-by-step Guide. New Riders. ISBN 978-0-7357-1434-2.
- Skinner, M. T. (1992). The Advanced C++ Book. Silicon Press. ISBN 978-0-929306-10-0.
- Love (1 September 2005). Linux Kernel Development. Pearson Education. ISBN 978-81-7758-910-8.
- DEHURI, SATCHIDANANDA; JAGADEV, ALOK KUMAR; RATH, AMIYA KUMAR (8 May 2007). OBJECT-ORIENTED PROGRAMMING USING C++. PHI Learning Pvt. Ltd. ISBN 978-81-203-3085-6.