KOTLIN / NOTES</>↗Kotlin20 Jun 2026
In Kotlin, classes and members are final by default, so you must explicitly mark them as open if you want them to be inherited or overridden. Basic class inheritance open class Animal { open fun makeSound() { println("Some sound") } } class Dog : Animal() { override fun makeSound() { println("Bark") } } Usage: fun […]
KOTLIN / NOTES</>↗Kotlin20 Jun 2026
In Kotlin, an init block runs setup logic when a class instance is created. It is commonly used to validate constructor arguments, initialize derived properties, or perform other construction-time setup. class User(val name: String, val age: Int) { init { require(name.isNotBlank()) { "Name must not be blank" } require(age >= 0) { "Age must be […]
KOTLIN / NOTES</>↗Kotlin20 Jun 2026
In Kotlin, you usually don’t manually override toString(), equals(), and hashCode() if your class is just a value/data holder. Use a data class instead. data class User( val id: Int, val name: String ) Kotlin automatically generates: toString() equals() hashCode() copy() componentN() Example: val a = User(1, "Alice") val b = User(1, "Alice") println(a) // […]
KOTLIN / NOTES</>↗Kotlin19 Jun 2026
In Kotlin, data classes are designed to store structured data with minimal boilerplate. A data class automatically provides useful functions such as: toString() equals() hashCode() copy() component functions for destructuring, like component1(), component2() Basic example data class User( val id: Int, val name: String, val email: String ) You can create and use it like […]
KOTLIN / NOTES</>↗Kotlin19 Jun 2026
In Kotlin classes, val and var are used to declare properties. val means read-only after initialization var means mutable / can be reassigned Basic example class User { val id: Int = 1 var name: String = "Alice" } Usage: fun main() { val user = User() println(user.id) // 1 println(user.name) // Alice user.name = […]
KOTLIN / NOTES</>↗Kotlin19 Jun 2026
In Kotlin, you usually define constructors and initialize properties directly in the class header using a primary constructor. 1. Primary constructor The most common style is: class Person(val name: String, var age: Int) This defines: a class named Person a read-only property name a mutable property age a constructor that requires both values Usage: fun […]