Inheritance in Kotlin

From the Kotlin cheat sheet · Classes & Objects · verified Jul 2026

Inheritance

Class inheritance and polymorphism

kotlin
// Open class (can be inherited)
open class Animal(val name: String) {
    open fun makeSound() {
        println("Some generic animal sound")
    }

    fun sleep() {
        println("$name is sleeping")
    }
}

// Inheritance
class Dog(name: String, val breed: String) : Animal(name) {
    override fun makeSound() {
        println("$name barks: Woof!")
    }

    fun wagTail() {
        println("$name is wagging tail")
    }
}

// Abstract classes
abstract class Shape {
    abstract val area: Double
    abstract fun draw()

    fun describe() {
        println("Area: $area")
    }
}

class Circle(private val radius: Double) : Shape() {
    override val area = Math.PI * radius * radius

    override fun draw() {
        println("Drawing circle with radius $radius")
    }
}
💡 Classes and members are final by default, use open to allow inheritance
⚡ Sealed classes enable exhaustive when expressions
📌 Interfaces can have default implementations
🟢 Use abstract classes for shared state, interfaces for contracts

Continue with Kotlin

Save the full cheat sheet or work through every related task.

More Kotlin tasks

Back to the full Kotlin cheat sheet