[SOLVED] Unable to implement an interface


#1

I have a maven project, two actually, three if you count the parent. Any I have set up an interface class like this in the nlp-api project:

package net.trajano.jee.nlp

interface Nlp {
	fun analyze(text: String)
}

In the nlp project where I do the implementation I have a class as follows:

package net.trajano.jee.nlp.impl

import edu.stanford.nlp.simple.Document
import net.trajano.jee.nlp.Nlp

class CoreNlp : Nlp {
	override fun analyze(text: String) {
		val doc = Document(text)
		for (sent in doc.sentences()) {
			// ...
		}
	}
}

However, I am getting the following error

D:\p\jee\nlp\src\main\java\net\trajano\jee\nlp\impl\CoreNlp.kt
ERROR: Class ‘CoreNlp’ must be declared abstract or implement abstract member public abstract fun analyze(text: String): Unit defined in net.trajano.jee.nlp.Nlp (6, 1)
ERROR: ‘analyze’ overrides nothing (7, 2)

If I relocated them to the same project (still different packages) it still does not work.

Am I missing something? I was following the https://kotlinlang.org/docs/reference/interfaces.html document


#2

Your code is correct. I think, you have problem with wrong JARs. You installed nlp-api with empty Nlp interface earlier, and now nlp project uses that nlp-api jar from ~/.m2/repsitory/... instead of new one. Use mvn install for your nlp-api project and try again.


#3

It didn’t work… thankfully if it did then there’d be something really wrong :slight_smile:

Anyway I tried to move it to the same package and it worked (I’ll update the OP with this fact so people don’t have to go all the way down here to read it)


#4

Ok I found the reason… this code

package net.trajano.jee.nlp

interface Nlp {
	fun analyze(text: String)
}

compiles with no error on Maven builds without the kotlin-stdlib probably because it was so simplistic… I noticed it when I had an Array and it failed to find it.

Once I added the kotlin-stdlib dependency to the API it compiled with no issues.

Perhaps the compiler should explicitly fail if it detects that kotlin stdlib is not in the dependency using https://maven.apache.org/ref/3.2.3/apidocs/org/apache/maven/project/MavenProject.html#getDependencies()