KOTLIN / CATATAN</>↗Kotlin21 Jun 2026
In Kotlin, visibility modifiers control where classes, properties, functions, and constructors can be accessed from. The main visibility modifiers are: public private protected internal If you do not specify a visibility modifier, Kotlin uses public by default. 1. public public means the declaration can be accessed from anywhere. class User { public val name: String […]
KOTLIN / CATATAN</>↗Kotlin21 Jun 2026
In Kotlin, interfaces define a contract that classes can implement. Kotlin supports multiple inheritance of interfaces, but not multiple inheritance of classes. 1. Defining an interface An interface can declare: abstract properties abstract functions functions with default implementations interface Drivable { val maxSpeed: Int fun drive() fun stop() { println("Stopping the vehicle") } } Here: […]
KOTLIN / CATATAN</>↗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 / CATATAN</>↗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 / CATATAN</>↗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 / CATATAN</>↗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 […]