How to overcome “same JVM signature” error when implementing a Java interface?

后端 未结 9 1827
星月不相逢
星月不相逢 2020-12-13 16:55

With the code below, I am getting the following error in IntelliJ IDEA 13.1.6 and Kotlin plugin 0.11.91.AndroidStudio.3:

Platform declaration clash: The foll         


        
相关标签:
9条回答
  • 2020-12-13 17:49

    We have found that to use the same names without clashing, the ctor args must be private AND you must still override the interfaces methods. You don't need any additional backing fields. Also, your expression body assignment will not recurse, so you can safely use that syntax.

    Java Interface

    interface IUser {
        String getUserScope();
        String getUserId();
    }
    

    Kotlin Class

    class SampleUser(private val userScope: String, private val userId: String) : IUser {
        override fun getUserId() = userId
        override fun getUserScope() = userScope
    }
    
    0 讨论(0)
  • 2020-12-13 17:51

    If you have direct control over the interface then the best approach is to write the interface in Kotlin. You can then write your class

    public class KotlinClass(override val name: String?) : KotlinInterface
    

    and still reference it from any Java code using the same interface as before. This looks a lot neater than setting all the properties to private and overriding the get function. Obviously if you can't migrate the interface to Java because you don't own it then that seems to be the only solution.

    0 讨论(0)
  • 2020-12-13 17:55

    Making that variable private solves the problem.

    public class KotlinClass(private val name: String?) : JavaInterface

    0 讨论(0)
提交回复
热议问题