How to Implement Inheritance In Kotlin?

9 minutes read

In Kotlin, inheritance can be implemented by using the : symbol followed by the name of the superclass after the class declaration. The derived class, also known as the subclass, inherits the properties and functions from the superclass.


For example, consider the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
open class Animal {
    open fun makesSound() {
        println("The animal makes a sound.")
    }
}

class Dog : Animal() {
    override fun makesSound() {
        println("The dog barks.")
    }
}


In this example, we have defined a superclass called Animal with a function makesSound(). The Dog class is created by inheriting from the Animal class using the : symbol. It overrides the makesSound() function to provide a different implementation.


To create an instance and call the overridden function:

1
2
3
4
5
6
7
fun main() {
    val animal = Animal()
    animal.makesSound() // Output: The animal makes a sound.

    val dog = Dog()
    dog.makesSound() // Output: The dog barks.
}


In the main() function, we create an instance of the Animal class and call its makesSound() function. Then, we create an instance of the Dog class and call the same function. The output reflects the overridden behavior defined in the Dog class.


This demonstrates the basic implementation of inheritance in Kotlin, where a subclass can inherit and override properties and functions from its superclass.

Best Kotlin Books to Read in 2024

1
Atomic Kotlin

Rating is 5 out of 5

Atomic Kotlin

2
Kotlin in Action

Rating is 4.9 out of 5

Kotlin in Action

3
Head First Kotlin: A Brain-Friendly Guide

Rating is 4.8 out of 5

Head First Kotlin: A Brain-Friendly Guide

4
Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

Rating is 4.7 out of 5

Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

5
Kotlin Cookbook: A Problem-Focused Approach

Rating is 4.6 out of 5

Kotlin Cookbook: A Problem-Focused Approach

6
Java to Kotlin: A Refactoring Guidebook

Rating is 4.5 out of 5

Java to Kotlin: A Refactoring Guidebook

7
Programming Kotlin: Create Elegant, Expressive, and Performant JVM and Android Applications

Rating is 4.4 out of 5

Programming Kotlin: Create Elegant, Expressive, and Performant JVM and Android Applications

8
Advanced Kotlin (Kotlin for Developers Book 4)

Rating is 4.3 out of 5

Advanced Kotlin (Kotlin for Developers Book 4)


How does inheritance work in Kotlin?

In Kotlin, inheritance works similarly to other object-oriented languages like Java.


To establish inheritance, you can use the open modifier to declare a class as open for being inherited. Then, to create a subclass, you can use the : symbol followed by the name of the superclass.


For example, consider a class called Animal:

1
2
3
4
5
open class Animal {
    open fun sound() {
        println("The animal makes a sound.")
    }
}


To inherit from this class, you can create a subclass called Dog:

1
2
3
4
5
class Dog : Animal() {
    override fun sound() {
        println("The dog barks loudly.")
    }
}


Here, the Dog class inherits the Animal class using the : symbol. Both classes have a sound() function, with the override keyword indicating that the function in the subclass is overriding the function in the superclass.


You can also override properties, constructors, and other members in a similar manner.


To create an instance of the Dog class and call its sound() function, you can use:

1
2
3
4
fun main() {
    val dog = Dog()
    dog.sound()
}


This would output: The dog barks loudly.


How to mark a method as final in Kotlin?

In Kotlin, to mark a method as final, you can use the final modifier. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
open class BaseClass {
    open fun myMethod() {
        println("BaseClass myMethod()")
    }
}

class DerivedClass : BaseClass() {
    final override fun myMethod() {
        println("DerivedClass myMethod()")
    }
}


In the example above, the myMethod() in the BaseClass is marked as open, which means it can be overridden by subclasses. In the DerivedClass, the myMethod() is marked with final to prevent further overrides, and it is also explicitly marked as override to indicate that it is overriding the base class method.


Note that the final modifier can only be used on methods in classes that are not marked as open. If a class is open, all its methods are open by default.


What is an abstract class in Kotlin?

In Kotlin, an abstract class is a class that cannot be instantiated directly and is meant to be inherited by other classes. It serves as a blueprint for subclasses to provide specific implementations for its abstract properties, methods, or functions.


An abstract class can include both abstract and non-abstract properties, methods, or functions. Abstract properties and methods are declared without an implementation and must be overridden by the subclasses. Non-abstract properties, methods, or functions can have an implementation and can be directly used or overridden by the subclasses.


To declare an abstract class in Kotlin, the "abstract" keyword is used in the class declaration. For example:


abstract class Animal { abstract fun eat() fun sleep() { println("Animal is sleeping") } }


In the above example, the "Animal" class is an abstract class with an abstract method named "eat()". The "sleep()" method is a non-abstract method with a default implementation. Any class that inherits from the "Animal" class will have to provide its own implementation for the "eat()" method.


What is hierarchical inheritance in Kotlin?

Hierarchical inheritance is a type of inheritance in Kotlin where a child class inherits properties and methods from a single parent class. However, the child class can also act as a parent class for another child class, thereby creating a hierarchical structure of inheritance.


In this type of inheritance, each child class has its own set of properties and methods, in addition to the ones inherited from the parent class. Each child class can further extend the functionality by adding new properties and methods or overriding the ones inherited from the parent class.


Hierarchical inheritance allows for the reuse of code and promotes code organization by grouping related classes together. It also provides flexibility for creating complex class hierarchies in Kotlin applications.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

Kotlin reflection allows you to inspect and manipulate code at runtime. Although Kotlin is fully compatible with Java, accessing Kotlin's reflection API from Java requires some extra steps.To use Kotlin reflection in Java, you need to follow these steps:Im...
The Kotlin Standard Library functions are a set of utility functions that come built-in with the Kotlin programming language. These functions provide convenient ways to perform common tasks when working with different types of data.To use the Kotlin Standard L...
Reading a file in Kotlin involves several steps. Here is a simple explanation of how you can read a file in Kotlin:Import the required classes: To read a file, you need to import the necessary classes. In Kotlin, you can use the java.io.File and kotlin.io.read...