Object Expression in Kotlin

Sometimes we need to create an object of some class with slight modification, without explicitly declaring a new subclass for it. Java handles this case with anonymous inner classes. Kotlin uses object expression to achieve the same functionality.

This post was originally posted at https://agrawalsuneet.github.io/blogs/object-expression-in-kotlin/ and reposted on Medium on 6th Oct 2018.

We can even create an object expression for an interface or abstract class by just implementing their abstract methods. This functionality is called an anonymous interface implementation or anonymous abstract class implementation.

Let’s understand with examples

We have a class AClass having someFunction in it. We want to pass AClass object to achieve some other functionality but at the same time, we want to override the default functionality of someFunction. Ideally, this can be done by creating another class extending AClass and overriding the someFunction method and then pass the object of extending class. But we can achieve the same using an anonymous inner class in Java.

public class AClass {

Now to use the method in UsageClass,

UsageClass usageClass = new UsageClass();

We created an object of AClass and passed it to useSomeClass method also we overridden the functionality of somefunction without extending the class.

The same can be achieved in Kotlin using object expression.

open class AClass {
open fun someFunction() {
println("SomeClass : some Function")

Now to use the method in UsageClass,

val usageClass = UsageClass()

Same can be possible in the case of anonymous interface implementation or anonymous abstract class implementation also.

Let’s have a look.

public abstract class AAbstractClass {
abstract void doSomething();

public void iAmDoingSomething() {
System.out.println("AAbstractClass : I am doing something");

public interface IInterface {
void canIDoSomething();

Now to use the functions in the UsageClass,

UsageClass usageClass = new UsageClass();

The equivalent Kotlin code for the same using object expression will be,

abstract class AAbstractClass {
internal abstract fun doSomething()

fun iAmDoingSomething() {
println("AAbstractClass : I am doing something")

interface IInterface {
fun canIDoSomething()

To use the UsageClass,

val usageClass = UsageClass()

If a supertype has a constructor, appropriate constructor parameters must be passed to it. Many supertypes may be specified as a comma-separated list after the colon :

open class BClass(val x: Int){
open val y = 20

We can even create just an object with no nontrivial supertypes. This is called an anonymous object.

val justAnObject = object {
var x = 10
var y = 20
fun sum(): Int = x + y

But the anonymous objects can only be used as types only in local and private declarations. If you use an anonymous object as a return type of a public function or the type of a public property, the actual type of that function or property will be the declared supertype of the anonymous object, or Any if you didn’t declare any supertype. In that case, the members added in the anonymous object will not be accessible.

class CClass {
// Private function, so the return type is
// the anonymous object type
private fun privateFunction() = object {
val x: String = "x"

// Public function, so the return type is Any
fun publicFunction() = object {
val x: String = "x"

fun main() {
val x1 = privateFunction().x // Works fine
val x2 = publicFunction().x // ERROR:
//Unresolved reference 'x'

One last thing, unlike Java we can access non-final variables also inside an object expression.

int value = 0;

In Kotlin, this works fine

var value = 0

usageClass.useInterface(object : IInterface {
override fun canIDoSomething() {
value++ // works fine

That’s all for now. You can read my other interesting posts here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.

Reference: Kotlin docs



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store