Getting Platform declaration clash when using Interface in kotlin

Kristy Welsh :

I'm converting some classes in Java to kotlin and I'm running into compile errors when trying to inherit from an interface:

Platform declaration clash: The following declarations have the same JVM signature (getContentID()Ljava/lang/String;):

public open fun get-content-id (): String? public open fun getContentId(): String?

Here is the interface:

interface Media {
  val contentId: String

  val displayRunTime: String

  val genres: List<String>

  val programId: String

  val runTime: String

  val type: String
}

here is the class:

class Airing : Media {
  override val contentId: String? = null
  override val displayRunTime: String? = null
  override val genres: List<String>? = null
  override val programId: String? = null
  override val runTime: String? = null
  override val type: String? = null

  override fun getContentId(): String? {
    return contentId
  }

I'm super new to kotlin.

hotkey :

You don't need to declare override fun getContentId(): String?, because the val contentId: String from the Media interface is already overridden by override val contentId: String?.

The error that you get means that the function you declare clashes in the JVM bytecode with the getter that is already generated for the contentId property (the getter has the same signature).

In Kotlin, you should work with the property contentId directly, whereas in Java you can use the generated accessors getContentId() and setContentId(...).

Also, Kotlin does not allow you to override a not-null String property with a nullable String? one, because the users of the base interface will expect a not-null value from the property. You should replace the overridden property types with String, or make them String? in the interface.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=468665&siteId=1