🐦 Kotlin
Advanced
What is an object expression (anonymous object) in Kotlin?
Answer
An object expression creates an anonymous class instance on the fly, without declaring a named class. It can implement interfaces or extend a class: val listener = object : View.OnClickListener { override fun onClick(v: View) { handleClick() } }. Unlike Java anonymous classes, Kotlin object expressions can implement multiple interfaces simultaneously. Object expressions capture the surrounding scope (they are closures), so they can access local variables. Each use of an object expression creates a new anonymous class. Note: object expressions are different from object declarations (which are singletons).
Previous
How does Kotlin handle Java interoperability with nullable types?
Next
What is the contract in Kotlin?