Tech moves fast! Stay ahead of the curve with Techopedia!
Join nearly 200,000 subscribers who receive actionable tech insights from Techopedia.
Operator overloading is a technique by which operators used in a programming language are implemented in user-defined types with customized logic that is based on the types of arguments passed.
Operator overloading facilitates the specification of user-defined implementation for operations wherein one or both operands are of user-defined class or structure type. This helps user-defined types to behave much like the fundamental primitive data types. Operator overloading is helpful in cases where the operators used for certain types provide semantics related to the domain context and syntactic support as found in the programming language. It is used for syntactical convenience, readability and maintainability.
Java does not support operator overloading, except for string concatenation for which it overloads the + operator internally.
Operators are referenced using operator notation in expressions and functional notation in declarations. The syntax, precedence and associativity of an operator cannot be changed by using user-defined operator declarations.
For example, operators in C# are categorized based on the type of operation they perform. Some of them are overloaded in user-defined types by defining them in static member functions and using the operator keyword. The parameters of the operator function represent the operands, while the return type of an operator function represents the result of the operation. Overloading is implemented in pairs for operators, such as comparison operators like "==" and "!=". The Equals() method for these operators should be overridden to avoid a compiler warning. Overloading is not to be used for operators such as conditional, logical ( and !!), assignment (+=, -=, etc.), casting and array indexing ().
It is highly recommended that operator overloading be implemented such that the results produced are what are intuitively expected out of the default implementation of the operator. Certain mathematical rules such as commutative law, applicable to expressions with two operands, cannot be applicable when used in overloading for types as they are only defined for numerical operands.
Do you work in the tech industry? Help us learn more about why the gender gap still exists in tech by taking this quick survey! Survey respondents will also be entered to win a $100 Amazon Gift Card!