Anonymous Objects
Inline implementations.
Anonymous Objects is a free Kotlin Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Inline Implementations
An anonymous object creates an instance of a class or interface without naming a new class. Use the object expression.
interface Greeter { fun greet(): String }
fun main() {
val g = object : Greeter {
override fun greet() = "Hi"
}
println(g.greet())
}Implementing an Interface
An object expression can implement one or more interfaces right where it is needed.
interface Clickable { fun click() }
fun main() {
val button = object : Clickable {
override fun click() = println("Clicked")
}
button.click()
}Overriding Class Members
You can subclass an open class anonymously and override members.
open class Animal { open fun sound() = "..." }
fun main() {
val cat = object : Animal() {
override fun sound() = "Meow"
}
println(cat.sound())
}Holding Extra State
An anonymous object may declare its own properties beyond what it overrides.
fun main() {
val point = object {
val x = 3
val y = 4
}
println("${point.x}, ${point.y}")
}Object Without a Supertype
If you omit a supertype, the object is just an ad-hoc bundle of members, useful for a quick local grouping.
fun main() {
val config = object {
val retries = 3
val timeout = 30
}
println(config.retries)
}Capturing Local Variables
Anonymous objects can capture variables from the enclosing scope, unlike static nested classes.
fun makeCounter(start: Int): () -> Int {
var count = start
val obj = object {
fun next() = count++
}
return { obj.next() }
}
fun main() {
val c = makeCounter(10)
println(c())
println(c())
}As a Listener
A common use is passing an inline implementation as a callback or listener.
interface OnDone { fun done(result: Int) }
fun compute(cb: OnDone) {
cb.done(42)
}
fun main() {
compute(object : OnDone {
override fun done(result: Int) = println("Got $result")
})
}Type of an Anonymous Object
When returned from a function, the inferred type is the declared supertype (or Any if none). Extra members are only visible locally.
interface Named { val name: String }
fun make(): Named = object : Named {
override val name = "X"
}
fun main() {
println(make().name)
}Anonymous Objects vs Lambdas
For single-method interfaces a lambda is shorter, but anonymous objects are needed when you must override multiple members or hold state.
interface Handler {
fun onStart()
fun onStop()
}
fun main() {
val h = object : Handler {
override fun onStart() = println("start")
override fun onStop() = println("stop")
}
h.onStart()
h.onStop()
}Object Expression vs Object Declaration
An object expression creates a fresh instance each time. An object declaration (object Name {}) is a named singleton. They look similar but differ in lifetime.
object Singleton { val id = 1 }
fun main() {
val fresh = object { val id = 2 }
println(Singleton.id)
println(fresh.id)
}A Compact Comparator
Anonymous objects are handy for one-off comparators or strategies.
fun main() {
val cmp = object : Comparator<Int> {
override fun compare(a: Int, b: Int) = b - a
}
val list = listOf(1, 3, 2).sortedWith(cmp)
println(list)
}Quick Check
Test your understanding of anonymous objects.
Recap
You learned about anonymous objects:
- The
object : Type {}expression creates an unnamed instance. - They can implement interfaces, subclass open classes, and hold state.
- They capture enclosing local variables.
- Use them for multi-method callbacks; prefer lambdas for single methods.
interface Run { fun go() }
fun main() {
val r = object : Run { override fun go() = println("go") }
r.go()
}Frequently asked questions
Is the “Anonymous Objects” lesson free?
Yes — the full text of “Anonymous Objects” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “Anonymous Objects”?
Inline implementations. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Anonymous Objects” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Nested Classes
- Inner Classes
- Anonymous Objects
- Local Classes