Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 917
Logo Avatar asked Jul 18 '17 13:07

Logo


People also ask

Can we extend final class in Kotlin?

yes, you can. Kotin extension method provides the ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern such as Decorator. So an extension method in Kotlin is using delegation rather than inheritance.

How do you inherit final class in Kotlin?

In Kotlin, unlike Java, all the classes are implicitly marked final by default. If you want to inherit from a class, you have to explicitly add open keyword before the class declaration.

How do I extend my Kotlin class?

In Kotlin we use a single colon character ( : ) instead of the Java extends keyword to extend a class or implement an interface. We can then create an object of type Programmer and call methods on it—either in its own class or the superclass (base class).

How do I make a class inheritable in Kotlin?

Any has three main methods that all classes inherit: equals(), hasCode(), toString() . All Kotlin classes are final, so they cannot be inherited. To make a class inheritable, the keyword open needs to be present at the beginning of the class signature, which also makes them non-final.


1 Answers

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

like image 128
Girish Arora Avatar answered Sep 20 '22 03:09

Girish Arora