Mobile Development
Kotlin
Subjective
Oct 04, 2025
Explain operator overloading in Kotlin with examples.
Detailed Explanation
Operator overloading allows defining custom behavior for operators on user-defined types using specific function names.\n\n**Arithmetic operators:**\n\ndata class Point(val x: Int, val y: Int) {\n operator fun plus(other: Point) = Point(x + other.x, y + other.y)\n operator fun minus(other: Point) = Point(x - other.x, y - other.y)\n operator fun times(scalar: Int) = Point(x * scalar, y * scalar)\n operator fun div(scalar: Int) = Point(x / scalar, y / scalar)\n operator fun unaryMinus() = Point(-x, -y)\n}\n\nval p1 = Point(1, 2)\nval p2 = Point(3, 4)\nval sum = p1 + p2 // Point(4, 6)\nval scaled = p1 * 3 // Point(3, 6)\nval negated = -p1 // Point(-1, -2)\n\n\n**Comparison operators:**\n\ndata class Version(val major: Int, val minor: Int, val patch: Int) : Comparable {\n override fun compareTo(other: Version): Int {\n return when {\n major != other.major -> major - other.major\n minor != other.minor -> minor - other.minor\n else -> patch - other.patch\n }\n }\n}\n\nval v1 = Version(1, 2, 3)\nval v2 = Version(1, 3, 0)\nprintln(v1 < v2) // true\nprintln(v1 >= v2) // false\n\n\n**Index access operators:**\n\nclass Matrix(private val data: Array) {\n operator fun get(row: Int, col: Int): Int {\n return data[row][col]\n }\n \n operator fun set(row: Int, col: Int, value: Int) {\n data[row][col] = value\n }\n}\n\nval matrix = Matrix(arrayOf(intArrayOf(1, 2), intArrayOf(3, 4)))\nval value = matrix[0, 1] // 2\nmatrix[1, 0] = 5\n\n\n**Invoke operator (function call):**\n\nclass Multiplier(val factor: Int) {\n operator fun invoke(value: Int): Int {\n return value * factor\n }\n}\n\nval double = Multiplier(2)\nval result = double(5) // 10 (calls invoke)\n\n\n**Contains operator:**\n\nclass Range(val start: Int, val end: Int) {\n operator fun contains(value: Int): Boolean {\n return value in start..end\n }\n}\n\nval range = Range(1, 10)\nprintln(5 in range) // true\nprintln(15 in range) // false\n\n\n**Iterator operators:**\n\nclass CountDown(val start: Int) {\n operator fun iterator() = object : Iterator {\n private var current = start\n \n override fun hasNext() = current > 0\n override fun next() = current--\n }\n}\n\nfor (i in CountDown(3)) {\n println(i) // 3, 2, 1\n}\n\n\n**Destructuring operators:**\n\ndata class Person(val name: String, val age: Int, val email: String) {\n operator fun component1() = name\n operator fun component2() = age\n operator fun component3() = email\n}\n\nval person = Person("John", 30, "john@email.com")\nval (name, age, email) = person // Destructuring\n\n\n**Augmented assignment:**\n\nclass MutablePoint(var x: Int, var y: Int) {\n operator fun plusAssign(other: MutablePoint) {\n x += other.x\n y += other.y\n }\n}\n\nval point = MutablePoint(1, 2)\npoint += MutablePoint(3, 4) // point is now (4, 6)\n\n\n**Operator precedence follows standard mathematical rules:**\n• Unary operators: +, -, !, ++, --\n• Multiplicative: *, /, %\n• Additive: +, -\n• Range: ..\n• Comparison: <, >, <=, >=\n• Equality: ==, !=\n• Logical: &&, ||
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts