Inheritance – Why is this class considered to be the ultimate?

I tried to inherit the following type, but the compiler says it is final.

class Dice(private var side: Int)
{
constructor(D: DiceTypesK): this(D.value) {}
}

class ExplodedDice(private val D: DiceTypesK): Dice (D)
// ^^^^ this class is final and
// can not be inherited from

Why my type is final because I don’t plan to do it ?

In Kotlin, unlike Java and C#, all classes are final by default, unless Clearly marked as open. This is the content of the docs:

The open annotation on a class is the opposite of Java’s final: it allows others to inherit from this class. By default, all classes in Kotlin are final, which corresponds to 07001, Item 17: Design and document for inheritance or else prohibit it.

The solution is just to mark your class as an open class Dice.

In addition, please pay attention to the limitations on data classes inheritance: they can neither be opened nor Extend another class.

I tried to inherit the following type, but the compiler says it is final.

< pre>class Dice(private var side: Int)
{
constructor(D: DiceTypesK): this(D.value) {}
}

class ExplodedDice (private val D: DiceTypesK): Dice(D)
// ^^^^ this class is final and
// can not be inherited from

Why is my type Ultimately, because I don’t plan to do this?

In Kotlin, unlike Java and C#, all classes are final by default, unless explicitly marked as open. This is the content of the docs:

The open annotation on a class is the opposite of Java’s final: it allows others to inherit from this class. By default, all classes in Kotlin are final, which corresponds to 07001, Item 17: Design and document for inheritance or else prohibit it.

The solution is just to The classes are marked as open class Dice.

In addition, please note that the limitations on data classes are inherited: they can neither be opened nor extended by another class.

Leave a Comment

Your email address will not be published.