kotlin エラー「error: this type has a constructor, and thus must be initialized here」の解決方法

kotlin エラー「error: this type has a constructor, and thus must be initialized here」の解決方法

kotlinで、エラー「error: this type has a constructor, and thus must be initialized here」の解決方法を記述してます。

環境

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

エラー全文

以下のコードで発生。

abstract class Hoge {

    abstract fun f()    

}

class Foo() : Hoge {

    override fun f() { println("Foo") }

}

fun main(){

    val bar = Foo()
    bar.f() // Foo

}

エラーメッセージ

error: this type has a constructor, and thus must be initialized here
class Foo() : Hoge {
              ^

原因

「サブクラス」で使用する「抽象クラス」に「()」を使用していないため

対処法

「()」を使用する

abstract class Hoge {

    abstract fun f()    

}

class Foo() : Hoge {

    override fun f() { println("Foo") }

}

fun main(){

    val bar = Foo()
    bar.f() // Foo

}