Operator Overloading

What Does Operator Overloading Mean?

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.

Advertisements

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.

Techopedia Explains Operator Overloading

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.

Advertisements

Related Terms

Latest DevOps Terms

Related Reading

Margaret Rouse

Margaret Rouse is an award-winning technical writer and teacher known for her ability to explain complex technical subjects to a non-technical, business audience. Over the past twenty years her explanations have appeared on TechTarget websites and she's been cited as an authority in articles by the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine and Discovery Magazine.Margaret's idea of a fun day is helping IT and business professionals learn to speak each other’s highly specialized languages. If you have a suggestion for a new definition or how to improve a technical explanation, please email Margaret or contact her…