Junior — Middle
Kas on võimalik deklareerida pärija Kotlin andmeklassis?
sobes.tech AI
Vastus AI-lt
By default, in Kotlin, data classes cannot be inherited because they are automatically declared as final. This is done to ensure the correct functioning of generated methods such as equals(), hashCode() and copy().
If you try to declare a class that inherits from a data class, the compiler will produce an error.
However, if it is really necessary, you can make a data class open by adding the open modifier, but this is not recommended and not directly supported, as a data class cannot be open.
Example that will not compile:
data class User(val name: String)
class AdminUser(val adminLevel: Int) : User("Admin") // Error
Therefore, inheritance from a data class in Kotlin is impossible.