Sobes.tech
Junior

What are the requirements when creating a data class in Kotlin?

sobes.tech AI

Answer from AI

  1. The primary constructor must have at least one parameter marked with val or var.
  2. The class cannot be abstract, open, sealed, or inner.
  3. The class cannot inherit from other classes.
  4. The class can implement interfaces.

The compiler automatically generates the following functions for data classes:

  • equals()
  • hashCode()
  • toString()
  • componentN() (for destructuring)
  • copy()