Computer scienceProgramming languagesKotlinObject-oriented programmingObject-oriented programming basic

Final members

3 minutes read

In this topic, we will discuss the concept of final members in the context of the Kotlin programming language. In programming languages such as Java and C++, the final keyword is used to indicate that a value, method, or class cannot be changed or overridden. In Kotlin, however, the situation is somewhat different.

Understanding 'final' in Kotlin

In Kotlin, all classes and methods are final by default. This means that if you declare a class in Kotlin, you will not be able to inherit it until you clearly make it open. Similarly, methods and properties in Kotlin are also final by default, and they cannot be overridden in subclasses until they are declared open.

Look at the example:

class MyFinalClass {
    fun myFinalMethod() {
        println("This method cannot be overridden!")
    }
}

class MyChildClass : MyFinalClass() { // Error! Cannot inherit MyFinalClass
    override fun myFinalMethod() { // Error! Cannot override myFinalMethod
        println("I'm trying to override your method!")
    }
}

In this example, we cannot inherit MyFinalClass or override myFinalMethod because both are final by default.

You might wonder why Kotlin takes such an approach. The answer is simple: the approach contributes to safer, more predictable code. After all, if a class or method can be arbitrarily inherited or overridden, that can lead to undesirable consequences. When you explicitly make a class or method open, it is a clear signal of your intention to allow inheritance or redefinition.

Let's take a closer look at the concept of final in Kotlin and look at some examples of its use.

'final' vs. 'open'

As you already know, in Kotlin, all classes and members of final classes are defaulted, which means they cannot be overridden. If you want your class to inherit, or a method to be overridden, you need to use the keyword open.

open class MyBaseClass {
    open fun myMethod() {
        println("Basic implementation")
    }
}

class MyDerivedClass : MyBaseClass() {
    override fun myMethod() {
        println("Overridden implementation")
    }
}

In this example, MyBaseClass and myMethod are declared open, so MyDerivedClass can inherit MyBaseClass and override myMethod.

'final' after 'open'

It is important to note that you can use final for overridden methods or properties to prevent them from being further redefined. Let's take an example:

open class MyBaseClass {
    open fun myMethod() {
        println("Basic implementation")
    }
}

open class MyIntermediateClass : MyBaseClass() {
    final override fun myMethod() {
        println("An overridden implementation that cannot be redefined further")
    }
}

class MyDerivedClass : MyIntermediateClass() {
    override fun myMethod() { // Error! Cannot override myMethod
        println("I'm trying to override your method!")
    }
}

In this example, myMethod in MyIntermediateClass is declared final, which means it cannot be overridden in MyDerivedClass.

Conclusion

In conclusion, final in Kotlin provides tighter control over inheritance and redefinition, which makes code more secure and manageable. This is one of the many features of Kotlin that make the language so attractive to developers.

57 learners liked this piece of theory. 1 didn't like it. What about you?
Report a typo