How do I use class inheritance with open and override in Kotlin?

In Kotlin, classes and members are final by default, so you must explicitly mark them as open if you want them to be inherited or overridden.

Basic class inheritance

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

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

Usage:

fun main() {
    val dog = Dog()
    dog.makeSound()
}

Output:

Bark

Key rules

1. Use open on a class to allow inheritance

open class Animal

Without open, this is not allowed:

class Dog : Animal()

because Animal would be final by default.

2. Use open on functions or properties to allow overriding

open fun makeSound() {
    println("Some sound")
}

If a function is not marked open, subclasses cannot override it.

3. Use override in the subclass

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

Kotlin requires override so it is clear that you are replacing behavior from the parent class.

Inheriting from a class with a constructor

If the parent class has a constructor, the subclass must call it:

open class Animal(val name: String) {
    open fun introduce() {
        println("I am $name")
    }
}

class Dog(name: String) : Animal(name) {
    override fun introduce() {
        println("I am a dog named $name")
    }
}

Usage:

fun main() {
    val dog = Dog("Buddy")
    dog.introduce()
}

Output:

I am a dog named Buddy

Overriding properties

Properties can also be open and override:

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

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

Usage:

fun main() {
    val dog = Dog()
    println(dog.sound)
}

Output:

Bark

Calling the parent implementation with super

You can call the superclass version using super:

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

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

Output:

Some sound
Bark

Preventing further overriding

An overridden member is open by default. If you want to prevent subclasses from overriding it again, mark it as final:

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

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

Now subclasses of Dog cannot override makeSound().

Summary

open class Parent {
    open fun method() {
        println("Parent method")
    }
}

class Child : Parent() {
    override fun method() {
        println("Child method")
    }
}
  • open class means the class can be inherited.
  • open fun or open val means the member can be overridden.
  • override means the subclass is replacing a parent member.
  • Use super.method() to call the parent version.

How do I use the init block to run setup logic in Kotlin classes?

In Kotlin, an init block runs setup logic when a class instance is created.

It is commonly used to validate constructor arguments, initialize derived properties, or perform other construction-time setup.

class User(val name: String, val age: Int) {

    init {
        require(name.isNotBlank()) {
            "Name must not be blank"
        }

        require(age >= 0) {
            "Age must be non-negative"
        }

        println("Created user: $name")
    }
}

Usage:

val user = User("Alice", 30)

When User("Alice", 30) is called, the init block runs automatically.

Key points

  • init blocks are part of the class initialization process.
  • They run after primary constructor parameters are available.
  • A class can have multiple init blocks.
  • Multiple init blocks run in the order they appear in the class body.
  • They are especially useful with primary constructors.

Example with multiple init blocks:

class Rectangle(val width: Int, val height: Int) {

    val area: Int

    init {
        require(width > 0) {
            "Width must be positive"
        }
    }

    init {
        require(height > 0) {
            "Height must be positive"
        }

        area = width * height
    }
}

You can also combine init blocks with property initializers:

class Product(val price: Double) {

    val tax = price * 0.2

    init {
        require(price >= 0) {
            "Price cannot be negative"
        }
    }
}

Initialization happens in the order the declarations appear in the class body, so property initializers and init blocks are executed top to bottom.

How do I override the toString(), equals(), and hashCode() methods in Kotlin?

In Kotlin, you usually don’t manually override toString(), equals(), and hashCode() if your class is just a value/data holder. Use a data class instead.

data class User(
    val id: Int,
    val name: String
)

Kotlin automatically generates:

toString()
equals()
hashCode()
copy()
componentN()

Example:

val a = User(1, "Alice")
val b = User(1, "Alice")

println(a)        // User(id=1, name=Alice)
println(a == b)   // true
println(a.hashCode() == b.hashCode()) // true

Manual override

If you need custom behavior, override them explicitly:

class User(
    val id: Int,
    val name: String
) {
    override fun toString(): String {
        return "User(id=$id, name=$name)"
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other !is User) return false

        return id == other.id && name == other.name
    }

    override fun hashCode(): Int {
        var result = id
        result = 31 * result + name.hashCode()
        return result
    }
}

Important notes

Use == for equality in Kotlin:

a == b

This calls equals() internally.

Use === for reference equality:

a === b

This checks whether both variables point to the exact same object.

Custom equality example

Sometimes you only want equality based on one property, such as id:

class User(
    val id: Int,
    val name: String
) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other !is User) return false

        return id == other.id
    }

    override fun hashCode(): Int {
        return id
    }

    override fun toString(): String {
        return "User(id=$id, name=$name)"
    }
}

The key rule is: if you override equals(), you should also override hashCode() consistently. Objects that are equal must have the same hash code.

How do I use data classes in Kotlin to store structured data?

In Kotlin, data classes are designed to store structured data with minimal boilerplate.

A data class automatically provides useful functions such as:

  • toString()
  • equals()
  • hashCode()
  • copy()
  • component functions for destructuring, like component1(), component2()

Basic example

data class User(
    val id: Int,
    val name: String,
    val email: String
)

You can create and use it like this:

fun main() {
    val user = User(
        id = 1,
        name = "Alice",
        email = "[email protected]"
    )

    println(user)
}

Output:

User(id=1, name=Alice, [email protected])

Accessing properties

println(user.name)
println(user.email)

Because the properties are declared in the primary constructor, they are available directly.

Comparing data objects

Data classes compare values, not object references:

val user1 = User(1, "Alice", "[email protected]")
val user2 = User(1, "Alice", "[email protected]")

println(user1 == user2) // true

Copying with changes

Use copy() to create a modified copy:

val updatedUser = user.copy(email = "[email protected]")

println(updatedUser)

The original object is unchanged.

Destructuring

Data classes support destructuring declarations:

val (id, name, email) = user

println(id)
println(name)
println(email)

Mutable vs immutable properties

Prefer val for immutable data:

data class Product(
    val id: Long,
    val name: String,
    val price: Double
)

Use var only if the property needs to change:

data class MutableUser(
    var name: String,
    var age: Int
)

Example with nested structured data

data class Address(
    val street: String,
    val city: String,
    val postalCode: String
)

data class Customer(
    val id: Int,
    val name: String,
    val address: Address
)

fun main() {
    val customer = Customer(
        id = 100,
        name = "Maria",
        address = Address(
            street = "Main Street",
            city = "Berlin",
            postalCode = "10115"
        )
    )

    println(customer.address.city)
}

Important rules

A Kotlin data class must:

  • Have at least one parameter in the primary constructor
  • Mark primary constructor parameters with val or var
  • Not be abstract, open, sealed, or inner

Example:

data class Book(
    val title: String,
    val author: String,
    val year: Int
)

Use data classes when you mainly need a class to hold data rather than define complex behavior.

How do I use val and var inside Kotlin classes?

In Kotlin classes, val and var are used to declare properties.

  • val means read-only after initialization
  • var means mutable / can be reassigned

Basic example

class User {
    val id: Int = 1
    var name: String = "Alice"
}

Usage:

fun main() {
    val user = User()

    println(user.id)      // 1
    println(user.name)    // Alice

    user.name = "Bob"     // OK: name is var

    // user.id = 2        // Error: id is val
}

val inside a class

Use val when the property should not be reassigned after it gets a value.

class Product {
    val sku: String = "ABC-123"
}

You can read it:

val product = Product()
println(product.sku)

But you cannot assign a new value:

// product.sku = "XYZ-999" // Not allowed

var inside a class

Use var when the property can change.

class Counter {
    var count: Int = 0

    fun increment() {
        count++
    }
}

Usage:

fun main() {
    val counter = Counter()

    counter.increment()
    counter.increment()

    println(counter.count) // 2
}

Declaring properties in the constructor

A common Kotlin style is to put properties directly in the class constructor.

class User(
    val id: Int,
    var name: String
)

This creates a class with:

  • a read-only id
  • a mutable name

Usage:

fun main() {
    val user = User(1, "Alice")

    println(user.id)
    println(user.name)

    user.name = "Bob" // OK

    // user.id = 2    // Error
}

Important distinction

If you write this:

val user = User(1, "Alice")

The variable user itself cannot point to another User, because it is a val.

But if the object has var properties, those properties can still change:

val user = User(1, "Alice")

user.name = "Bob" // OK, because name is var

// user = User(2, "Charlie") // Error, because user is val

So:

val user

means the reference cannot be reassigned.

var name

inside the class means the property can be changed.

Rule of thumb

Use val by default, and only use var when the value really needs to change.

class Person(
    val birthYear: Int,
    var displayName: String
)

Here, birthYear probably should not change, but displayName might.