0
0
Kotlinprogramming~10 mins

Operator overloading concept in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Operator overloading concept
Define class with operator function
Create objects of class
Use operator symbol between objects
Compiler calls operator function
Return result of operation
Use result in program
Operator overloading lets you use symbols like + or * with your own classes by defining special functions.
Execution Sample
Kotlin
data class Point(val x: Int, val y: Int) {
    operator fun plus(other: Point) = Point(x + other.x, y + other.y)
}

val p1 = Point(1, 2)
val p2 = Point(3, 4)
val p3 = p1 + p2
This code adds two Point objects using the + operator overloaded by the plus function.
Execution Table
StepActionEvaluationResult
1Create p1Point(1, 2)p1 = Point(1, 2)
2Create p2Point(3, 4)p2 = Point(3, 4)
3Evaluate p1 + p2Calls p1.plus(p2)Calls plus function
4Inside plus functionx + other.x = 1 + 3, y + other.y = 2 + 4Returns Point(4, 6)
5Assign result to p3p3 = Point(4, 6)p3 = Point(4, 6)
6EndNo more operationsExecution stops
💡 No more operations to perform, program ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 5Final
p1undefinedPoint(1, 2)Point(1, 2)Point(1, 2)Point(1, 2)
p2undefinedundefinedPoint(3, 4)Point(3, 4)Point(3, 4)
p3undefinedundefinedundefinedPoint(4, 6)Point(4, 6)
Key Moments - 3 Insights
Why does p1 + p2 call the plus function?
Because the plus function is marked with the operator keyword, Kotlin knows to use it when + is used between Point objects (see Step 3 in execution_table).
What happens if we don't define the plus operator function?
The + operator won't work with Point objects and will cause a compile error, since Kotlin doesn't know how to add them (no operator function called).
Does operator overloading change the meaning of + globally?
No, it only changes how + works for the specific class where you define the operator function (here, Point). Other types keep their usual + behavior.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of p3 after Step 5?
APoint(3, 4)
BPoint(1, 2)
CPoint(4, 6)
Dundefined
💡 Hint
Check the 'Assign result to p3' row in execution_table where p3 is set to Point(4, 6).
At which step does the plus function actually compute the new Point?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at Step 4 in execution_table where the plus function adds coordinates and returns Point(4, 6).
If we remove the operator keyword from the plus function, what happens when running p1 + p2?
ACompile error, + operator not recognized for Point
BIt works the same, calling plus function
CRuntime error when adding points
Dp1 and p2 are concatenated as strings
💡 Hint
Operator keyword tells Kotlin to use plus with +; without it, + is unknown for Point (see key_moments).
Concept Snapshot
Operator overloading in Kotlin lets you define how operators like + work with your classes.
Use the operator keyword before a function (e.g., operator fun plus(other: Type)).
When you use + between objects, Kotlin calls this function.
This makes your classes behave more naturally with operators.
Without operator keyword, operators won't work with your class.
Full Transcript
Operator overloading in Kotlin allows you to use standard operators like + with your own classes by defining special functions marked with the operator keyword. In the example, a Point class defines operator fun plus to add two points. When you write p1 + p2, Kotlin calls p1.plus(p2) behind the scenes, returning a new Point with summed coordinates. Variables p1 and p2 hold the original points, and p3 holds the result. This process is shown step-by-step in the execution table. Key points include that the operator keyword is necessary for Kotlin to recognize the function as an operator, and that operator overloading only affects the class where it is defined. Without it, using + on Point objects causes a compile error. This makes your classes easier and more natural to use with operators.