Overloading
Overloading in computer programming is a form of polymorphism, which means "can take many forms." What overloading means in practice is that the compiler of a language that supports it can distinguish between two or more different functions or operators that are represented by the same name or symbol.
In computer programming, a function is a self-contained sequence of code statements that perform a set of actions and return a value of a known type, and an operator is a symbol that represents a specific action. For example, a function called timeEgg() might contain code that times the boiling of an egg, and the "+" symbol is an operator that represents addition. Most programming languages also support other operators that allow the code to manipulate numbers and character strings in more complicated ways.
Every function in the same program must have a unique signature so the compiler can tell which one is being called at any point of the code. When there are two or more functions in the same program that have the same name but have different signatures, the function is said to be overloaded.
The compiler creates the function's signature by combining information about the name of the function; the type or order, or both, of the parameters passed when the function is called; it's visibility qualifiers, or information about what else in the program is allowed to see the function; and its return type. So long as each function has a signature that is different from every other function's signature in the program, it will be able to treat them as different functions even if their names are the same.
It is useful to understand that operators themselves can be considered to be functions in the same way as timeEgg() is a function, but the way they are presented to the compiler is different in order to make computer programs easier to read.
For example, the C++ programming language allows operators to be overloaded for derived classes, or classes that are a "kind of" another class. What this means is that it is possible to give different semantics or meaning to the expression b + c in the following code, depending on what type of data b and c represent.
- myClass a, b, c;
- a = b + c;
The interesting thing is that the programmer can arbitrarily decide what the b + c means by adding code to the "operator+()" function in the definition of the class myClass. The declaration might look something like this:
- class myClass {
- myClass& operator+(const myClass& obj);
- }
And the programmer can call it like the conventional function it is:
or using the syntactic shortcut the compiler allows:
Thus operator overloading and function overloading can be seen to boil down to much the same thing at the language level. Java does not allow operator overloading although it breaks its own rule in the single case of the String class, where the language definition means that the "+" operator is overloaded and it is legal to write Java code like this:
- String a = "hello, ";
- String b = "world!";
- String c = a + b; // c contains "hello, world!";
Overloading tends to be characteristic of languages where every item of data is strictly defined in terms of what it is and what it can do. Languages of this kind are called strongly typed languages. C++ and Java are both strongly typed languages.
Weakly typed languages allow programmers to be more relaxed in how they use data objects in the program. The C programming language is not very strongly typed, and scripting languages like Perl are practically untyped. In C the function signature is based on the name of the function alone, which makes it impossible for the C compiler to distinguish between functions that have the same name so it is impossible to have two functions with the same name in the same C program.
Function overloading is a useful addition to programming languages that helps programmers write more easily understood, and thus more easily and cheaply maintained programs.
This is the complete article, containing 669 words
(approx. 2 pages at 300 words per page).