Sling Academy
Home/Kotlin/What is Inheritance? Understanding the Basics in Kotlin

What is Inheritance? Understanding the Basics in Kotlin

Last updated: November 30, 2024

Inheritance is a fundamental concept in object-oriented programming that allows a class to inherit the properties and methods of another class, facilitating code reusability and the extension of existing functionalities. In Kotlin, inheritance is used to create a new class called a subclass that is based on an existing class called a superclass.

Understanding Inheritance in Kotlin

In Kotlin, to declare a class as inheritable, you must mark it with the open keyword. By default, all classes in Kotlin are final, which means they cannot be inherited unless explicitly specified as open.

Superclass Declaration

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

In this example, Animal is a superclass with an open method sound(). This method can be overridden by any subclass.

Subclass Declaration

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

The class Dog inherits from Animal and overrides the sound() method to provide its own implementation.

Using Subclasses

fun main() {
    val myDog: Animal = Dog()
    myDog.sound()  // Outputs: Bark
}

Here, we instantiate Dog but reference it as an Animal. When we call the sound() method, the overridden version in Dog gets executed, producing an output of Bark.

Key Points to Remember

  • Use the open keyword to make a class or method inheritable.
  • Use the override keyword to provide a specific implementation for a superclass's method in a subclass.
  • Kotlin supports single inheritance, where a subclass can have only one direct superclass. However, Kotlin supports interfaces to allow multiple inheritance-like behavior.

Benefits of Using Inheritance

Inheritance supports a hierarchical classification where you can:

  • Reduce code redundancy by reusing code in derived classes.
  • Enhance code readability and organization.
  • Effectively implement polymorphic behavior.

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

Previous Article: Exploring `init` Blocks for Class Initialization in Kotlin

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