WBF Academy
OOP Intermediate 2 min read

Object Expressions as Anonymous Classes

Create one-off implementations inline

Kotlin Academy

Kotlin Academy

Kotlin Engineer

5 months ago

Object expressions create anonymous class instances that can implement interfaces, extend classes, and capture variables from the enclosing scope. Unlike SAM conversions (which only work for single-abstract-method interfaces), object expressions work with any interface or class. They are the go-to tool for one-shot implementations of multi-method interfaces like listeners.

#kotlin #object-expression #anonymous-class #oop