Method (computer programming)

From Wikipedia for FEVERv2
Jump to navigation Jump to search

A method in object-oriented programming (OOP) is a procedure associated with a message and an object. Method (computer programming)_sentence_0

An object consists of data and behavior; these comprise an interface, which specifies how the object may be utilized by any of its various consumers. Method (computer programming)_sentence_1

Data is represented as properties of the object, and behaviors are represented as methods. Method (computer programming)_sentence_2

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. Method (computer programming)_sentence_3

In class-based programming, methods are defined in a class, and objects are instances of a given class. Method (computer programming)_sentence_4

One of the most important capabilities that a method provides is method overriding - the same name (e.g., area) can be used for multiple different kinds of classes. Method (computer programming)_sentence_5

This allows the sending objects to invoke behaviors and to delegate the implementation of those behaviors to the receiving object. Method (computer programming)_sentence_6

A method in Java programming sets the behavior of a class object. Method (computer programming)_sentence_7

For example, an object can send an area message to another object and the appropriate formula is invoked whether the receiving object is a rectangle, circle, triangle, etc. Method (computer programming)_sentence_8

Methods also provide the interface that other classes use to access and modify the properties of an object; this is known as encapsulation. Method (computer programming)_sentence_9

Encapsulation and overriding are the two primary distinguishing features between methods and procedure calls. Method (computer programming)_sentence_10

Overriding and overloading Method (computer programming)_section_0

Method overriding and overloading are two of the most significant ways that a method differs from a conventional procedure or function call. Method (computer programming)_sentence_11

Overriding refers to a subclass redefining the implementation of a method of its superclass. Method (computer programming)_sentence_12

For example, findArea may be a method defined on a shape class. Method (computer programming)_sentence_13

The various subclasses: rectangle, circle, triangle, etc. would each define the appropriate formula to calculate their area. Method (computer programming)_sentence_14

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. Method (computer programming)_sentence_15

This is known as encapsulation and is meant to make code easier to maintain and re-use. Method (computer programming)_sentence_16

Method overloading, on the other hand, refers to differentiating the code used to handle a message based on the parameters of the method. Method (computer programming)_sentence_17

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. Method (computer programming)_sentence_18

The following simple Java example illustrates the difference: Method (computer programming)_sentence_19

Accessor, mutator and manager methods Method (computer programming)_section_1

Accessor methods are used to read the data values of an object. Method (computer programming)_sentence_20

Mutator methods are used to modify the data of an object. Method (computer programming)_sentence_21

Manager methods are used to initialize and destroy objects of a class, e.g. constructors and destructors. Method (computer programming)_sentence_22

These methods provide an abstraction layer that facilitates encapsulation and modularity. Method (computer programming)_sentence_23

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 (e.g., a database fetch), without the dependent code needing to be changed. Method (computer programming)_sentence_24

The concepts of encapsulation and modularity are not unique to object-oriented programming. Method (computer programming)_sentence_25

Indeed, in many ways the object-oriented approach is simply the logical extension of previous paradigms such as abstract data types and structured programming. Method (computer programming)_sentence_26

Constructors Method (computer programming)_section_2

A constructor is a method that is called at the beginning of an object's lifetime to create and initialize the object, a process called construction (or instantiation). Method (computer programming)_sentence_27

Initialization may include an acquisition of resources. Method (computer programming)_sentence_28

Constructors may have parameters but usually do not return values in most languages. Method (computer programming)_sentence_29

See the following example in Java: Method (computer programming)_sentence_30

Destructors Method (computer programming)_section_3

A destructor is a method that is called automatically at the end of an object's lifetime, a process called destruction. Method (computer programming)_sentence_31

Destruction in most languages does not allow destructor method arguments nor return values. Method (computer programming)_sentence_32

Destruction can be implemented so as to perform cleanup chores and other tasks at object destruction. Method (computer programming)_sentence_33

Finalizers Method (computer programming)_section_4

In garbage-collected languages, such as Java, C#, and Python, destructors are known as finalizers. Method (computer programming)_sentence_34

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. Method (computer programming)_sentence_35

Abstract methods Method (computer programming)_section_5

An abstract method is one with only a signature and no implementation body. Method (computer programming)_sentence_36

It is often used to specify that a subclass must provide an implementation of the method. Method (computer programming)_sentence_37

Abstract methods are used to specify interfaces in some programming languages. Method (computer programming)_sentence_38

Example Method (computer programming)_section_6

The following Java code shows an abstract class that needs to be extended: Method (computer programming)_sentence_39

The following subclass extends the main class: Method (computer programming)_sentence_40

Reabstraction Method (computer programming)_section_7

If a subclass provides an implementation for an abstract method, another subclass can make it abstract again. Method (computer programming)_sentence_41

This is called reabstraction. Method (computer programming)_sentence_42

In practice, this is rarely used. Method (computer programming)_sentence_43

Example Method (computer programming)_section_8

In C#, a virtual method can be overridden with an abstract method. Method (computer programming)_sentence_44

(This also applies to Java, where all non-private methods are virtual.) Method (computer programming)_sentence_45

Interfaces' default methods can also be reabstracted, requiring subclasses to implement them. Method (computer programming)_sentence_46

(This also applies to Java.) Method (computer programming)_sentence_47

Class methods Method (computer programming)_section_9

Class methods are methods that are called on a class rather than an instance. Method (computer programming)_sentence_48

They are typically used as part of an object meta-model. Method (computer programming)_sentence_49

I.e, for each class, defined an instance of the class object in the meta-model is created. Method (computer programming)_sentence_50

Meta-model protocols allow classes to be created and deleted. Method (computer programming)_sentence_51

In this sense, they provide the same functionality as constructors and destructors described above. Method (computer programming)_sentence_52

But in some languages such as the Common Lisp Object System (CLOS) the meta-model allows the developer to dynamically alter the object model at run time: e.g., to create new classes, redefine the class hierarchy, modify properties, etc. Method (computer programming)_sentence_53

Special methods Method (computer programming)_section_10

Special methods are very language-specific and a language may support none, some, or all of the special methods defined here. Method (computer programming)_sentence_54

A language's compiler may automatically generate default special methods or a programmer may be allowed to optionally define special methods. Method (computer programming)_sentence_55

Most special methods cannot be directly called, but rather the compiler generates code to call them at appropriate times. Method (computer programming)_sentence_56

Static methods Method (computer programming)_section_11

Static methods are meant to be relevant to all the instances of a class rather than to any specific instance. Method (computer programming)_sentence_57

They are similar to static variables in that sense. Method (computer programming)_sentence_58

An example would be a static method to sum the values of all the variables of every instance of a class. Method (computer programming)_sentence_59

For example, if there were a Product class it might have a static method to compute the average price of all products. Method (computer programming)_sentence_60

In Java, a commonly used static method is: Method (computer programming)_sentence_61

This static method has no owning object and does not run on an instance. Method (computer programming)_sentence_62

It receives all information from its arguments. Method (computer programming)_sentence_63

A static method can be invoked even if no instances of the class exist yet. Method (computer programming)_sentence_64

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. Method (computer programming)_sentence_65

Copy-assignment operators Method (computer programming)_section_12

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. Method (computer programming)_sentence_66

Operator methods Method (computer programming)_section_13

Operator methods define or redefine operator symbols and define the operations to be performed with the symbol and the associated method parameters. Method (computer programming)_sentence_67

C++ example: Method (computer programming)_sentence_68

Member functions in C++ Method (computer programming)_section_14

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. Method (computer programming)_sentence_69

Perhaps the most well-known example is C++, an object-oriented extension of the C programming language. Method (computer programming)_sentence_70

Due to the design requirements to add the object-oriented paradigm on to an existing procedural language, message passing in C++ has some unique capabilities and terminologies. Method (computer programming)_sentence_71

For example, in C++ a method is known as a member function. Method (computer programming)_sentence_72

C++ also has the concept of virtual functions which are member functions that can be overridden in derived classes and allow for dynamic dispatch. Method (computer programming)_sentence_73

Virtual functions Method (computer programming)_section_15

Virtual functions are the means by which a C++ class can achieve polymorphic behavior. Method (computer programming)_sentence_74

Non-virtual member functions, or regular methods, are those that do not participate in polymorphism. Method (computer programming)_sentence_75

C++ Example: Method (computer programming)_sentence_76

See also Method (computer programming)_section_16

Method (computer programming)_unordered_list_0


Credits to the contents of this page go to the authors of the corresponding Wikipedia page: en.wikipedia.org/wiki/Method (computer programming).