KOTLIN / NOTES</>↗Kotlin21 Jun 2026
Object expressions vs. object declarations in Kotlin Kotlin has two closely related features: Object expressions: create an anonymous object immediately. Object declarations: create a named singleton object. They both use the object keyword, but they are used for different purposes. 1. Object expressions Use an object expression when you need a one-off object, often to […]
KOTLIN / NOTES</>↗Kotlin21 Jun 2026
In Kotlin, a companion object is an object declared inside a class that can hold members callable on the class itself, giving you static-like behavior. Kotlin does not have Java-style static members directly. Instead, you usually use companion object. Basic example class User(val name: String) { companion object { const val DEFAULT_NAME = "Guest" fun […]
KOTLIN / NOTES</>↗Kotlin21 Jun 2026
In Kotlin, classes can be declared inside other classes in two main ways: Nested classes — default behavior Inner classes — declared with the inner keyword Nested classes A class declared inside another class is nested by default. class Outer { class Nested { fun message(): String { return "Hello from Nested" } } } […]
KOTLIN / NOTES</>↗Kotlin21 Jun 2026
In Kotlin, an abstract class is a class that cannot be instantiated directly. It is meant to be subclassed. An abstract method is a method declared without an implementation. Subclasses must override it. Basic example abstract class Animal { abstract fun makeSound() fun sleep() { println("Sleeping…") } } class Dog : Animal() { override fun […]
KOTLIN / NOTES</>↗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 / NOTES</>↗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: […]