Content deleted Content added
→top: make less class-centric |
m per WP:HYPHEN, sub-subsection 3, points 3,4,6, replaced: commonly- → commonly, typo(s) fixed: For example → For example, (2) using AWB |
||
Line 1:
A '''method''' (or '''message''') in [[object-oriented programming]] (OOP) is a [[Procedure (computer science)|procedure]] associated with an [[Object (computer science)|object]]. An object is made up of data and behavior, which form the interface that an object presents to the outside world. Data is represented as [[Property (programming)|properties]] of the object and behavior as methods. For example, a <code>Window</code> object would have methods such as <code>open</code> and <code>close</code>, while its state (whether it is opened or closed) would be a property.
In [[class-based programming]], methods are defined in a [[class (computer science)|class]], and objects are instances of a given class. One of the most important capabilities that a method provides is method overriding. The same name (e.g., <code>area</code>) 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. Method in Java programming sets the behaviour of class object. For example, an object can send an <code>area</code> message to another object and the appropriate formula will be invoked whether the receiving object is a <code>rectangle</code>, <code>circle</code>, <code>triangle</code>, etc.
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 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, <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
Line 97:
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 an instance for a class. For example, if there were a <code>Product</code> class it might have a static method to compute the average price of all products.
In Java, a commonly
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.<ref>{{Cite book|title = Clean Code: A Handbook of Agile Software Craftsmanship|last = Martin|first = Robert C.|publisher = Prentice Hall|year = 2009|isbn = 978-0-13-235088-4|___location = |pages = 296|author-link = Robert Cecil Martin}}</ref>
|