← All courses

Classes, Data Classes and Coroutines

🗓 May 31, 2026 ⏱ 1 min read

Classes and data classes

data class User(val id: Int, val name: String)

val u = User(1, "Anand")
println(u.name)            // Anand
val u2 = u.copy(name = "Gaur")  // easy copies

A data class automatically gives you equals, toString and copy — perfect for models.

Coroutines: easy background work

Coroutines let you run long tasks (network, database) without freezing the screen, written in a simple top-to-bottom style.

suspend fun loadUser(): User {
    delay(1000)            // pretend network call
    return User(1, "Anand")
}

lifecycleScope.launch {
    val user = loadUser()  // waits without blocking the UI
    println(user.name)
}
Key word: suspend marks a function that can pause and resume — the heart of coroutines.