How to "accept" accidential ovverride



I have the following interface:

interface FeatureInformationFileEntry {

fun getGitHash(): String

fun getPreprocessorFeatures(): String


I want to use a data class (without class body!) to implement this interface

internal  class Size2Entry(val gitHash: String, val preprocessorFeatures: String) : FeatureInformationFileEntry

I know that there is an “accidential” override, but how can I allow this override? Or what would be the Kotlin way to achieve this?

Thank you!

Florian Reisinger


You have three options:

  • make the interface declare the val
  • make the class implement the method rather than the val
  • write the interface in Java to allow the compatibility behaviour (but note that it breaks in certain cases)


Coming from Java I do not have a good feeling towards letting the interface decide for an implementation (using Val).

When using the val it will also generate a getter. I want to save the variables coming in (thus the var, as it should not be modifyable) and implement the getter. So basically I did the flash on purpose.

Writing the interface in Java is a nice idea, sad that this is not possible within Kotlin… Will try that! Thank you


Have tried the “interface in Java” approach, still same error


Declaring the val in the interface is basically merely requiring the getter. When implementing it you can even use a var to override it (possibly with a private setter). Note that kotlin properties only have a backing field if the field is assigned (if the getter provides the value another way that is possible).

I think that you have a misunderstanding about the ways properties can be declared (btw using the convert from java function can be very helpful). In this case you want something like:

class Size2Entry(gitHash: String, preprocessorFeatures: String) {
    var gitHash:String = gitHash
        get() {...}
        private set

Basically to override the getters and setters you need to declare the property in the body of the class. The parameters to the primary constructor are available to all instance initialisation code.


Thank you :smiley: I got the concept a bit more. When using val it was easy to override another val… Still for me it should be possible to allow the accidential overwrite. But maybe I am not thinking Kotlin enough :stuck_out_tongue: