0

I am interesting are there any way to replace kotlin interface variable in Java.

E.g. what should i write in java to have same logic

interface ObjectWithId
{
    var id : Long
}

Before facing this problem all my code could be easily converted back to java, but using interface variables breaks 'backward compatibility'. Isn't it?

2
  • I'm not clear if you're asking how to create an implementation of this specific interface in your Java code, or how to define an interface in Java with the same functionality as this one. Commented Mar 29, 2020 at 0:50
  • 1
    One helpful site for these question. It provides a kotlin compiler + java disassembler javap.yawk.at/#O0tyIG/procyon Commented Mar 29, 2020 at 1:13

1 Answer 1

4

In java this interface would be:

public interface ObjectWithId {
    long getId();        
    void setId(long p0);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for answer, and also i should have some variable in 'implementer' to store id?
This is the correct answer, the var or val in Kotlin are not actually variables, but properties (a setter, getter and backing field joined together). Interfaces are not supposed to hold state, see this.
@Bios90 thats open for the implementor, how this property is implemented. Most common would be a private field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.