Monday 25 February 2013

Operator Overloading

   It is an approach of defining multiple behaviors to an operator just like we can define multiple behaviors to a method in method overloading.
   In method overloading a method will have different behaviors that are defined basing on the parameter types of that method. In the same way in operator overloading, a operator will have different behaviors based on the operands between which we use the operators.
   For example '+' is an overloaded operator which works as additional operator when used between two numeric operands and works as a concatenation operator when used with the string operands or string-numeric operands.
numeric+numeric=addition
string+string=concatenation
string+numeric=concatenation
   Same as we discussed about we can also add new behavior to  existing operators of the language by defining an operator method on that operator as following:
[<modifiers>] static <types> operator <opt> (<operand types>)
    A operator method must b defined as static only. Type refers to the data-type of the value what it returns when the operator is used between two operands. Operator is a keyword which tells we are defining an operator method. <opt> refers to the type of operands between which we want to use the operator.

No comments:

Post a Comment