Sling Academy
Home/Kotlin/Overriding Methods and Properties in Kotlin

Overriding Methods and Properties in Kotlin

Last updated: November 30, 2024

Understanding Overriding in Kotlin

Kotlin allows you to override both methods and properties, providing flexibility to developers. Overriding enables derived classes to provide specific implementations of functions or properties that are defined in base classes. Let's dive into how this works in Kotlin, with examples to illustrate the process.

Overriding Methods

To override a function in a derived class, the function in the base class must be marked with the open keyword, which indicates that it can be overridden. The overridden function in the derived class is then marked with the override keyword.


open class Animal {
    open fun sound() {
        println("Animal makes a sound")
    }
}

class Dog : Animal() {
    override fun sound() {
        println("Dog barks")
    }
}

fun main() {
    val dog: Animal = Dog()
    dog.sound()  // Output: Dog barks
}

In this example, the Animal class has a method sound that can be overridden. The Dog class provides its own implementation of the sound method.

Overriding Properties

Similar to methods, properties can also be overridden. To allow a property to be overridden in the base class, it needs to be marked with the open keyword. In the derived class, it must use the override keyword.


open class Parent {
    open val info: String = "I am a parent"
}

class Child : Parent() {
    override val info: String = "I am a child"
}

fun main() {
    val child = Child()
    println(child.info)  // Output: I am a child
}

Here, the Parent class defines an info property, which is overridden in the Child class.

Final Methods and Properties

If you want to prevent a method or property from being overridden further, you can use the final keyword. In Kotlin, by default, all methods and properties are implicitly final unless specifically marked as open.


open class Bird {
    open fun fly() { println("Bird flies") }
    final fun sing() { println("Bird sings") }
}

class Sparrow : Bird() {
    override fun fly() { println("Sparrow flies swiftly") }
    // Cannot override sing() because it is final
}

In the example above, the sing method cannot be overridden in the Sparrow class because it is marked as final.

Conclusion

Overriding is a powerful feature in object-oriented programming that allows developers to extend and provide specific implementations for functions and properties in derived classes. Kotlin's use of open and override keywords provides developers with clear guidance for overriding, while the final keyword provides control over inheritance.

Next Article: Kotlin: Using the `super` Keyword to Access Parent Class Members

Previous Article: How to Extend a Class in Kotlin Using `open`

Series: Kotlin Object-Oriented Programming

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin