Content deleted Content added
m +es: +pt: |
Technobadger (talk | contribs) merge in Constructor overloading and add some links |
||
Line 1:
'''Method overloading''' is a feature found in various [[object oriented]] [[programming language]]s such as [[C Sharp#|C#]], [[C++]] and [[Java (programming language)|Java]] that allows the creation of several [[subprogram|function]]s with the same name which differ from each other in terms of the type of the input and the type of the output of the function.
An example of this would be a [[Square (algebra)|square]] function which takes a number and returns the square of that number. In this case, it is often necessary to create different functions for [[Integer (computer science)|integer]] and [[floating point]] numbers.
Method overloading is usually associated with [[static typing|statically-typed]] programming languages which enforce [[type checking]] in [[function
Method overloading should not be confused with [[ad-hoc polymorphism]] or [[virtual function]]s. In those, the correct method is chosen at runtime.
In most languages [[Constructor (computer science)|constructors]] can also be overloaded, so that there can be more than one constructor for a class, each having different parameters.
Constructor overloading allows multiple ways of creating an object using constructors with different parameter types and numbers of [[parameter (computer science)|parameters]].
For example, a class <tt>Person<tt> that holds data about each person's name and phone number. In the example below, written in [[Java (programming language)|Java]], the constructor is ''overloaded'' as there are two constructors for the class <tt>Person<tt>.
<pre>
public class Person {
// instance variables:
String name;
String phoneNumber;
// Constructor with name and number:
public Person(String name, String phoneNumber){
this.name=name;
this.phoneNumber=phoneNumber;
}
// Alternative constructor without giving phone number.
// This will set the phone number to "N/A" (for "not available")
public Person(String name){
this(name, "N/A");
}
}
</pre>
In Java, the call <tt>this.name=name</tt> means "set this object's variable called name to the value of the argument ''name''.
Note that the second constructor invokes the first one with the call <tt>this(name, "N/A")</tt>. It must be called on the first line of the constructor and can only be called once per constructor. This is generally considered a good programming practice. It might seem more natural and clear to write the second constructor above as:
<pre>
public Person(String name){
this.name=name;
this.phoneNumber="N/A";
}
</pre>
This would work the same way as the first example. The benefit of calling one constructor from another is that it provides code that is more flexible. Suppose that the names needed to be stored in lower case only. Then only one line would need to be changed:
this.name=name;
in the first constructor is changed to
this.name=name.toLowerCase();
If the same line was repeated in the second constructor, then it would also need to be changed to produce the desired effect.
==See also==
*[[Factory method pattern]]
*[[Object-oriented programming]]
*[[Constructor (computer science)|Constructor]]
*[[Abstraction (computer science)|Abstraction]]
*[[Overriding]]
▲*[[Constructor overloading]]
|