Method (computer programming): Difference between revisions

Content deleted Content added
Reverted 1 edit by 174.99.134.242 (talk): ?
Link suggestions feature: 2 links added.
 
(14 intermediate revisions by 8 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>
 
===DestructorsDestructor===
{{Main|Destructor (computer science)}}
A ''[[Destructor (computer science)|destructorDestructor]]'' is a method that is called automatically at the end of an object's lifetime, a process called [[object lifetime|destructionDestruction]]. Destruction in most languages does not allow destructor method arguments nor return values. DestructionDestructors 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===
Line 179:
== References ==
{{refbegin}}
*{{cite book|url=https://books.google.com/books?id=DnsM0WD-6iMC&pg=PA131|title=C++ ANDand OBJECTObject-ORIENTEDoriented PROGRAMMINGProgramming PARADIGMParadigm|last=JANA|first=DEBASISH|date=1 January 2005|publisher=PHI Learning Pvt. Ltd.|isbn=978-81-203-2871-6}}
*{{cite book|url=https://books.google.com/books?id=ZLzt5WtsdzIC&pg=PA50|title=Object-Oriented Programming: Fundamentals And Applications|last=Sengupta|first=Probal|date=1 August 2004|publisher=PHI Learning Pvt. Ltd.|isbn=978-81-203-1258-6}}
*{{cite book|url=https://books.google.com/books?id=Miq73i_J1i4C&pg=PA36|title=Object-oriented Programming: Using C++ for Engineering and Technology|last=Svenk|first=Goran|publisher=Cengage Learning|year=2003|isbn=0-7668-3894-3}}