-1

I am implemending a java interface(Command) with this method:

void setInputStream(InputStream in);

I want to override this with a Kotlin setter:

class ProxyCommand : Command {
    lateinit var _inputStream: ChannelPipedInputStream

    var inputStream: InputStream
        get() = this._inputStream
        set(value) { // This should override it.
            this._inputStream = (value as ChannelPipedInputStream)
        }
}

But I am getting this error at set(value):

Accidental override: The following declarations have the same JVM signature (setInputStream(Ljava/io/InputStream;)V): 
public final fun <set-inputStream>(value: InputStream): Unit defined in ...
public abstract fun setInputStream(`in`: InputStream!): Unit defined in ...

It says that it is an accidental override, but it is not accidental...

Is what I want possible? Or do I have to just override the setInputStream method. I like the kotlin setter more..

Jan Wytze
  • 3,307
  • 5
  • 31
  • 51

1 Answers1

-1

Or do I have to just override the setInputStream method. I like the kotlin setter more..

tldr: yes

It's not possible for a property setter to override a function of the supertype Command. As a workaround, make your property private and implement the required interface methods in addition as follows e.g.:

class ProxyCommand : Command {
    override fun setInputStream(`in`: InputStream) {
        TODO("not implemented")
    }

    private lateinit var inputStream: ChannelPipedInputStream
}
s1m0nw1
  • 76,759
  • 17
  • 167
  • 196