codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

13 / 68 topics
11Extensions Functions12Classes and Objects13Inheritance in Kotlin14Interfaces in Kotlin15Data Classes16Sealed Classes17Companion Objects18Generics in Kotlin19Type Aliases20Properties and Fields21Lateinit Properties22Delegated Properties
Tutorials/Kotlin/Inheritance in Kotlin
🎯Kotlin

Inheritance in Kotlin

Updated 2026-04-20
3 min read

Inheritance in Kotlin

Inheritance is a fundamental concept in object-oriented programming that allows a class (known as a subclass or derived class) to inherit properties and behaviors from another class (known as a superclass or base class). This promotes code reusability, modularity, and hierarchical classification of classes. Kotlin supports inheritance with some unique features compared to other languages like Java.

Understanding Inheritance

In Kotlin, all classes are final by default, meaning they cannot be inherited unless explicitly marked as open. This is different from Java, where classes are non-final by default.

Declaring an Open Class

To allow a class to be inherited, you must declare it with the open keyword:

open class Animal {
    open fun makeSound() {
        println("Some generic animal sound")
    }
}

Basic Inheritance

A subclass can inherit from a superclass using the : symbol followed by the superclass name. The subclass can override methods and properties of the superclass.

Example: Basic Inheritance

open class Animal {
    open fun makeSound() {
        println("Some generic animal sound")
    }
}

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

fun main() {
    val dog = Dog()
    dog.makeSound()  // Output: Bark!
}

Overriding Methods

To override a method, the override keyword is used. The overridden method must have the same signature as the method in the superclass.

open class Animal {
    open fun makeSound() {
        println("Some generic animal sound")
    }
}

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

Overriding Properties

Properties can also be overridden, but the property must be marked as open in the superclass.

open class Animal {
    open val sound: String = "Some generic animal sound"
}

class Dog : Animal() {
    override val sound: String = "Bark!"
}

Constructor Inheritance

Kotlin supports primary and secondary constructors. When a subclass has its own constructor, it must initialize the superclass using super.

Example: Primary Constructor Inheritance

open class Animal(val name: String) {
    open fun makeSound() {
        println("Some generic animal sound")
    }
}

class Dog(name: String) : Animal(name) {
    override fun makeSound() {
        println("Bark!")
    }
}

Example: Secondary Constructor Inheritance

open class Animal(val name: String) {
    open fun makeSound() {
        println("Some generic animal sound")
    }
}

class Dog : Animal {
    constructor(name: String) : super(name) {
        // Additional initialization code
    }

    override fun makeSound() {
        println("Bark!")
    }
}

The super Keyword

The super keyword is used to call the superclass's methods or properties.

Example: Using super in Method Overriding

open class Animal {
    open fun makeSound() {
        println("Some generic animal sound")
    }
}

class Dog : Animal() {
    override fun makeSound() {
        super.makeSound()
        println("Bark!")
    }
}

Example: Using super in Property Initialization

open class Animal(val name: String) {
    open val description: String = "A generic animal"
}

class Dog(name: String) : Animal(name) {
    override val description: String = "${super.description} that barks"
}

Inheritance Hierarchy

Kotlin supports multiple levels of inheritance, allowing for complex class hierarchies.

Example: Multi-Level Inheritance

open class Animal {
    open fun makeSound() {
        println("Some generic animal sound")
    }
}

open class Mammal : Animal() {
    override fun makeSound() {
        println("A mammal makes a sound")
    }
}

class Dog : Mammal() {
    override fun makeSound() {
        println("Bark!")
    }
}

Abstract Classes and Methods

Abstract classes cannot be instantiated on their own and must be inherited. They can contain abstract methods, which do not have an implementation.

Example: Abstract Class

abstract class Animal {
    abstract fun makeSound()
}

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

Sealed Classes

Sealed classes are used to restrict the inheritance hierarchy. They can only be extended within the same file.

Example: Sealed Class

sealed class Result {
    data class Success(val data: String) : Result()
    data class Error(val message: String) : Result()
}

fun handleResult(result: Result) {
    when (result) {
        is Result.Success -> println("Success: ${result.data}")
        is Result.Error -> println("Error: ${result.message}")
    }
}

Best Practices

  1. Use Inheritance Sparingly: Overuse of inheritance can lead to complex and hard-to-maintain code. Prefer composition over inheritance when possible.
  2. Keep Classes Open for Extension, Closed for Modification: Design your classes in a way that they are easy to extend but difficult to break existing functionality.
  3. Avoid Deep Inheritance Hierarchies: A deep hierarchy can be difficult to manage and understand. Aim for a flat or shallow hierarchy where possible.

Conclusion

Inheritance is a powerful feature in Kotlin that promotes code reuse and hierarchical classification of classes. By understanding how to declare open classes, override methods and properties, use constructors, and leverage abstract and sealed classes, you can effectively utilize inheritance in your Kotlin applications. Remember to apply best practices to maintain clean and manageable code.

This tutorial provides a comprehensive guide to inheritance in Kotlin, covering the basics through advanced concepts. By following this guide, you should have a solid understanding of how to implement and use inheritance in your Kotlin projects.


PreviousClasses and ObjectsNext Interfaces in Kotlin

Recommended Gear

Classes and ObjectsInterfaces in Kotlin