Operator Overloading

Why Trust Techopedia

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

Margaret Rouse
Technology expert
Margaret Rouse
Technology expert

Margaret is an award-winning writer and educator known for her ability to explain complex technical topics to a non-technical business audience. Over the past twenty years, her IT definitions have been published by Que in an encyclopedia of technology terms and cited in articles in the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine, and Discovery Magazine. She joined Techopedia in 2011. Margaret’s idea of ​​a fun day is to help IT and business professionals to learn to speak each other’s highly specialized languages.