We have already seen that the '+' operatior is used to add to numbers.
fun main() { var x = 5 var y = 7 var z = x + y println("The added result is : "+z) }
That was quite easy.
Now, let us say, we have a class that has an integer value. And we want to create two objects and add them.
Let me make it a little simpler with the below example.
class MyClass(x: Int) { var x: Int = x } fun main() { var obj1 = MyClass(5) var obj2 = MyClass(7) var obj3 = obj1 + obj2 println("The added result "+obj3) }
So, in the above code, we have created a class named 'MyClass' that has an attribute named 'x'. And we have defined a Constructor that initialises the attribute 'x'.
class MyClass(x: Int) { var x: Int = x }
Then we have created an object 'obj1' of the class 'MyClass'.
var obj1 = MyClass(5)
And initialised the value of 'x' with '5'(For object 'obj1').
Similarly, we have created a second object 'obj2'.
var obj2 = MyClass(7)
And initialised the value of 'x' with '7'(For object 'obj2').
Now, we tried to add two objects 'obj1' and 'obj2'. Just to add '5' and '7'.
var obj3 = obj1 + obj2
But we end up with the below error.
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
This is because Kotlin is confused that how can adding two objects add their values?
With this we come to the concept of Overloading the operators. i.e. We will make the '+' operator act the way, we want it to act.
Let us modify the above example.
class MyClass(x: Int) { var x: Int = x operator fun plus(obj: MyClass): MyClass { return MyClass(this.x + obj.x) } } fun main() { var obj1 = MyClass(5) var obj2 = MyClass(7) var obj3 = obj1 + obj2 println("The added result "+obj3.x) }
And all we have done is, defined a method called 'operator fun plus(obj: MyClass): MyClass'
operator fun plus(obj: MyClass): MyClass { return MyClass(this.x + obj.x) }
This 'plus()' method is already defined by Kotlin. When we specify 'operator' before the 'plus()' Function. Kotlin understands, the 'plus()' method needs to be Overloaded.
And all we are doing is, redefining the 'plus()' method. So that it works exactly the way we want it to work.
So, the 'operator fun plus(obj: MyClass): MyClass' has one parameter, 'obj'.
Now, the 'plus()' method gets called when the below statement executes.
var obj3 = obj1 + obj2
And what happens is, the value of the object, 'obj1' is assigned to 'this' and 'obj2' is assigned to 'obj'.
And right now, 'this' has the contents of 'obj1',
And 'obj' has the contents of 'obj2',
Now, since, the 'plus()' method has the contents,
operator fun plus(obj: MyClass): MyClass { return MyClass(this.x + obj.x) }
The 'return' statement,
return MyClass(this.x + obj.x)
Adds the value of 'this.x' i.e. '5' and 'obj.x' i.e. '7'.
And the added value is returned to 'obj3'.
obj3 = obj1 + obj2
So, the print statement,
println("The added result "+obj3.x)
Prints the value of 'x' from the object 'obj3'. And we get the below output.
The added result 12
But have you thought, when we are executing the statement,
var obj3 = obj1 + obj2
How come the 'operator fun plus(obj: MyClass): MyClass' gets called?
This is because, for every operator, be it a '+' or '-' or any other operator, Kotlin uses a method for each. And each method begins with operator.
So, for '+', Kotlin has the 'operator fun plus(..)' method already defined. And all we are doing is, redefining the 'operator fun plus(..)' in our class.
class MyClass(x: Int) { var x: Int = x operator fun plus(obj: MyClass): MyClass { return MyClass(this.x + obj.x) } }
So, when Kotlin finds the '+' operator for the objects of the class 'MyClass'.
var obj3 = obj1 + obj2
At first, it searches for the 'operator fun plus(obj: MyClass): MyClass' in the class 'MyClass'.
And changes,
var obj3 = obj1 + obj2
To,
var obj3 = obj1.plus(obj2)
And when it finds it.
operator fun plus(obj: MyClass): MyClass { return MyClass(this.x + obj.x) }
It executes the task.
Similarly, for subtraction, multiplication and division Kotlin has the below Overloaded method.
Operator | Expression | Overloaded Method |
---|---|---|
Addition | p1 + p2 | p1.plus(p2) |
Subtraction | p1 - p2 | p1.minus(p2) |
Division | p1 / p2 | p1.div(p2) |
Multiplication | p1 * p2 | p1.times(p2) |
So, if you want to overload, any of the above operators and customise it your way. You can use the corresponding overloaded method for it.