Object expression
An object expression is equivalent to Java's anonymous class. It is used to instantiate objects that might inherit from some class or that implements an interface. A classic use case is when we need to define objects that are implement an interface. This is how in Java we could implement the ServiceConnection
interface and assign it to a variable in Java:
ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { ... } @Override public void onServiceConnected(ComponentName name, IBinder service) { ... } }
The closest Kotlin equivalent of the preceding implementation is the following:
val serviceConnection = object: ServiceConnection { override fun onServiceDisconnected(name: ComponentName?) { } override fun onServiceConnected(name: ComponentName?, service: IBinder...