How to override Java-style getters and setters in Kotlin

Posted by Java developer blog on January 20, 2020

Overview

Sometimes you may need to implement a Java interface for a Kotlin class. In the post, we are going to discuss how to override Java-style getters and setters for a Kotlin class.

Example

Suppose we have the following Java interface:

public interface A {
  Long getId();

  boolean isNew();
}

Because Kotlin properties do not override Java-style getters and setters you have to add a private modifier for property and override a method in a Kotlin class:

class B(
  private val id: Long,
  private val new: Boolean
): A {
    override fun getId() = id

    override fun isNew() = new
}

Conclusion

We have described how to override Java-style getters and setters for a Kotlin class.