Overload (programming): Difference between revisions

Content deleted Content added
Glasnt (talk | contribs)
creating article.
 
No edit summary
Line 1:
To ''overload'' a method in [[programming]] is to have two or more methods with the same name, but are distingished by the '''number and/or type''' of variables they require.
 
For example, ''doTask()'' and ''doTask(object O)'' are '''overloaded''' methods. To call the latter, an [[object]] must be passed as a [[parameter]], whereas the former does not require a [[parameter]], and is called with an empty parameter field.
A common error would be to assign a default value to the object in the second method, this would result in an ''ambiguous call'' error, as the compiler wouldn't know wich of the two methods to use.
 
Another example would be a ''Print(object O)'' method. In this case we would like the [[method]] to be diferent when printing, for example, text and diferent for pictures. We write the two diferent methods as overloaded: ''Print(text_object T); Print(image_object P)''. If we write the overloaded print methods for all objects our program will "print", we never have to worry about the type of the object, and the correct [[function]] call again, the call is allways: ''Print(something)''.
{{compu-prog-stub}}