kotlin エラー「error: this class does not have a constructor」の解決方法

kotlin エラー「error: this class does not have a constructor」の解決方法

kotlinで、エラー「error: this class does not have a constructor」の解決方法を記述してます。インターフェイスに括弧「()」を使用している場合などに発生します。

環境

  • OS windows11 home
  • java 19.0.1
  • kotlin 1.7.20-release-201

エラー全文

以下のコードで発生。

interface Hoge {

    fun f() { print("A") }

}

class Foo() : Hoge() {

    override fun f() { println("こんにちわ") }

}


fun main(){

    val foo = Foo()
    foo.f() // こんにちわ
    
}

エラーメッセージ

error: this class does not have a constructor
class Foo() : Hoge() {
                  ^

原因

インターフェイスを実装するクラスで、インターフェイスに「()」を使用しているため

対処法

実装側の「Hoge」にある「()」を外す

interface Hoge {

    fun f() { print("A") }

}

class Foo() : Hoge {

    override fun f() { println("こんにちわ") }

}


fun main(){

    val foo = Foo()
    foo.f() // こんにちわ
    
}