Kotlin Activity cannot be extended. This type is final, so it cannot be inherited

KotlinKotlin Android-Extensions

Kotlin Problem Overview


I have created a Kotlin Activity, but I am not able to extend the activity. I am getting this message: This type is final, so it cannot be inherited from. How to remove final from Kotlin's activity, so it can be extended?

Kotlin Solutions


Solution 1 - Kotlin

As per Kotlin documentation, 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.

open class Base {
    open fun v() {}
    fun nv() {}
}

class Derived() : Base() {
    override fun v() {}
}

Refer :https://kotlinlang.org/docs/reference/classes.html

Solution 2 - Kotlin

By default the Kotlin activity is final, so we cannot extend the class. To overcome that we have to make the activity open so that, it can be extendable.

as like open class BaseCompatActivity : AppCompatActivity() { }

Solution 3 - Kotlin

In Kotlin, the classes are final by default that's why classes are not extendable.

> 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. Kotlin - Inheritance

open class Base(p: Int)

class Derived(p: Int) : Base(p)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLogoView Question on Stackoverflow
Solution 1 - KotlinGirish AroraView Answer on Stackoverflow
Solution 2 - KotlinLogoView Answer on Stackoverflow
Solution 3 - KotlinWaqar UlHaqView Answer on Stackoverflow