October 2019
Intermediate to advanced
434 pages
11h 54m
English
To create single, unnamed instances of a class, Kotlin introduces the notion of object expressions. To create an object expression, we use the object keyword followed by : and then any class or interface names we want to use:
val someInterface = object : SomeInterface { override fun doSomething(foo: String) { super.doSomething(foo) }}
Object declarations can implement multiple interfaces and have access to the variables defined within the scope in which they were created. It's also possible to use an object expression to create an unnamed class with no supertype. In this example, we assign an object expression to transient, in which we've declared a single property:
fun main(args: Array<String>) { val transient = object ...
Read now
Unlock full access