Method (computer programming): Difference between revisions

Content deleted Content added
Copetan (talk | contribs)
m removing title bolding
Revert to revision 911104023 dated 2019-08-16 14:48:52 by 2620:149:E0:6001:0:0:0:E0 using popups
Line 7:
Methods also provide the interface that other classes use to access and modify the data [[Property (programming)|properties]] of an object. This is known as encapsulation. Encapsulation and overriding are the two primary distinguishing features between methods and procedure calls.<ref>{{cite web|title=What is an Object?|url=http://docs.oracle.com/javase/tutorial/java/concepts/object.html|work=oracle.com|publisher=Oracle Corporation|accessdate=13 December 2013}}</ref>
 
==Overriding and Overloadingoverloading==
[[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, <code>findArea</code> may be a method defined on a shape class. The various subclasses: <code>rectangle</code>, <code>circle</code>, <code>triangle</code>, 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.<ref>[http://www.codeproject.com/Articles/16407/METHOD-Overload-Vs-Overriding http://www.codeproject.com/Articles/16407/METHOD-Overload-Vs-Overriding]</ref> The following simple [[Java language|Java]] example illustrates the difference:<ref>{{cite web
| accessdate = 2011-08-12
| author = John Suzuki
Line 39:
</syntaxhighlight>
 
==Accessor, Mutatormutator and Managermanager methods==
Accessor methods are used to read data values of an object. Mutator methods are used to modify the data of an object. Manager methods are used to initialize and destroy objects of a class, e.g. constructors and destructors.
 
Line 62:
A ''[[Destructor (computer science)|destructor]]'' is a method that is called automatically at the end of an object's lifetime, a process called [[object lifetime|destruction]]. Destruction in most languages does not allow destructor method arguments nor return values. Destruction can be implemented so as to perform cleanup chores and other tasks at object destruction.
 
====<big>Finalizers</big>====
In [[Garbage collection (computer science)|garbage-collected]] languages, such as [[Java (programming language)|Java]], [[C Sharp (programming language)|C#]], 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. Abstract methods are used to specify [[Interface (computing)|interfaces]] in some computer 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|accessdate=11 December 2014}}</ref>