One of the more advanced features in Kotlin is operator overloading. Learn operator overloading and how it allows newly created types to be used more naturally in Kotlin.
- [Instructor] In programming, operators are functions that use symbolic names. Take for example the basic math operators add and subtract. Those symbols call functions named plus and minus respectively. We can create our own functions for symbols in our classes. This is called operator overloading. Kotlin's operators can be roughly divided in three groups. First, there are the binary operators. They consist of an operator in the middle of two operands. Function name is what the operator is mapped to internally.
Next, there are the unary operators. They consist of an operator and a single operand. Finally, there are all of the rest of the operators. Currently there are more than 30 operators that you can overload. See the Kotlin documentation for more information. Let's create an overloaded operator. First we'll create a data class named position. We'll put it in its own file, so we'll right click, say new Kotlin file class.
We're going to call this position. Then what we'll do is we'll bring in our package and we'll create a data class, so data class position. We'll have a var x. This is going to be a type int. We'll have another var y and this is going to be of type int as well.
Now, in order to overload an operator, we first have to say operator. That's a key word here. Then we have to say fun for function and finally which operator we want to overload. In our case, what we want to overload is the plus operator. Then we'll say what are we taking in in this function. In this case we're going to take other, which is also of type position, and our function's going to return type position as well.
Then what we'll say is return, position and what we'll do is we'll say x plus other x and y plus other y. That's how we get it to do the addition. So, we've created a data class named position. It has two properties, x and y and it also has an overloaded plus operator. In order to use this, let's go back to the main function and let's just stick this in a function by itself.
We'll say fun, use, overload, open and close parentheses and we'll just make some classes that are using it. We'll say val p1 = position and we'll give it an x and y of 200, 100. We'll say val p2 = position, and this time we'll say 1000, 2000.
Finally, we'll say val p3 = p1 + p2, so this is our overloaded operator, our overloaded plus operator. We can say print line p3. Then from within our main function we need to call that, so we'll say use overload and then we'll run.
Here we can see the results of our addition. Here's our position class. It's got an x of 1200, so if you say 200 plus 1000, that's 1200, and our y's got 2100, 2000 plus 100, that is 2100. We show that we're overloading the plus sign on the position class. Keep in mind you should only use operator overloading for its intended purpose. If you make the plus function do multiplication, for example, you'll confuse people and possibly even yourself.
- Kotlin as a better Java
- Setting up a Kotlin programming environment
- Val vs. var
- Understanding basic Kotlin programming concepts
- Object-oriented programming
- Using Java from Kotlin
- Using Kotlin from Java
- Annotations, reflection, and DSL construction
- Functional programming in Kotlin