Method (computer programming): Difference between revisions

Content deleted Content added
Link suggestions feature: 2 links added.
 
(7 intermediate revisions by 5 users not shown)
Line 1:
{{short description|Function that is tied to a particular instance or class}}
 
A '''method''' in [[object-oriented programming]] (OOP) is a [[Procedure (computer science)|procedure]] associated with an [[Object (computer science)|object]], and generally also a [[Message passing|message]]. An object consists of ''state data'' and ''behavior''; these compose an [[Interface (computingobject-oriented programming)|''interface'']], which specifies how the object may be used. A method is a behavior of an object parametrized by a user.
 
Data is represented as [[Property (programming)|properties]] of the object, and behaviors are represented as methods. For example, a <code>Window</code> object could have methods such as <code>open</code> and <code>close</code>, while its state (whether it is open or closed at any given point in time) would be a property.
Line 36:
</syntaxhighlight>
 
===Big BallsDestructor===
{{Main|Big BallsDestructor (computer science)}}
A ''[[Destructor (computer science)|Big BallsDestructor]]'' is a method that is called automatically at the end of an object's lifetime, a process called [[object lifetime|Big BallsDestruction]]. Big ballsDestruction in most languages does not allow big ballsdestructor method arguments nor return values. Big BallsDestructors can be implemented so as to perform cleanup chores and other tasks at object destruction.
 
====Finalizers====
In [[Garbage collection (computer science)|garbage-collected]] languages, such as [[Java (programming language)|Java]],<ref name=Bloch>{{cite book | title= "Effective Java: Programming Language Guide" |last=Bloch| first=Joshua| publisher=Addison-Wesley | edition=third | isbn=978-0134685991| year=2018}}</ref>{{rp|26, 29}} [[C Sharp (programming language)|C#]],<ref name=Albahari>{{cite book |last=Albahari |first=Joseph |title= C# 10 in a Nutshell |publisher= O'Reilly |isbn= 978-1-098-12195-2}}</ref>{{rp|208-209}} and [[Python (programming language)|Python]], destructors are known as ''[[finalizer]]s''. 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 [[method signature|signature]] and no [[method body|implementation body]]. It is often used to specify that a subclass must provide an implementation of the method, as in an [[Class (computer programming)#Abstract and concrete|abstract class]]. Abstract methods are used to specify [[Interface (computing)|interfaces]] in some programming languages.<ref>{{cite web|title=Abstract Methods and Classes|url=http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html|website=oracle.com|publisher=Oracle Java Documentation|access-date=11 December 2014}}</ref>
 
===Example===
Line 102:
 
==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===