Inheritance in Kotlin

All Kotlin topics
Last updated: Jun 11, 2026
∙ Topic

Inheritance in Kotlin

Inheritance in Kotlin teaches you how to model behavior with focused Kotlin types. This lesson combines idiomatic Kotlin, a runnable JVM example, and production-focused guidance.

📝Syntax
class Item(val value: Int) {
    fun doubled() = value * 2
}
inheritance-in-kotlin.kt
📝 Edit Code
👁 Kotlin Output
💡 Edit the Kotlin code, compile it, and inspect the output.
👁Expected Output
42
🌎Real-World Uses
  • 1Inheritance in Kotlin appears in Android, backend, desktop, and multiplatform applications.
  • 2Teams use this concept to reduce boilerplate while preserving type safety.
  • 3It supports concise APIs that remain readable during maintenance.
  • 4Understanding it improves debugging and code review quality.
  • 5It helps Kotlin applications evolve without unnecessary mutation.
Common Mistakes
  • 1Using var when val communicates the intent better.
  • 2Forcing nullable values instead of handling absence safely.
  • 3Launching asynchronous work without lifecycle or cancellation rules.
  • 4Creating large classes with mixed responsibilities.
  • 5Using clever scope-function chains that hide control flow.
Best Practices
  • 1Prefer val and immutable collections by default.
  • 2Use null-safe operators and explicit domain types.
  • 3Keep functions small and use named arguments where they improve clarity.
  • 4Use structured concurrency for asynchronous work.
  • 5Run formatting, static analysis, and automated tests.
💡Core idea
  • 1Inheritance in Kotlin should make intent visible through Kotlin types and expressions.
  • 2Nullability is part of the type system.
  • 3Concise syntax should improve clarity rather than hide behavior.
  • 4A small runnable example verifies assumptions quickly.
💡How to apply it
  • 1Start with immutable values and focused data classes.
  • 2Model optional data with nullable types or sealed results.
  • 3Keep Android and backend lifecycle boundaries explicit.
  • 4Test normal, boundary, and failure paths.
💡Reliability checks
  • 1Avoid !! except where an invariant is proven.
  • 2Do not leak coroutine scopes or Android contexts.
  • 3Keep blocking work away from UI and request threads.
  • 4Validate external data before mapping it into domain objects.
💡Practice path
  • 1Retype and run the example.
  • 2Change one value and predict the output.
  • 3Replace mutation with an immutable transformation.
  • 4Extract reusable behavior into a focused function.
📋Quick Summary
  • Inheritance in Kotlin is a practical Kotlin concept.
  • val and null-safety reduce common defects.
  • Data classes and sealed types model domains clearly.
  • Structured concurrency improves asynchronous reliability.
  • Tests and static analysis support safe refactoring.
🎯Interview Questions
Q1. What is the purpose of Inheritance in Kotlin?
Answer: It helps developers model behavior with focused Kotlin types while keeping Kotlin code concise and type-safe.
Q2. What is the difference between val and var?
Answer: val is a read-only reference, while var allows reassignment.
Q3. How does Kotlin null-safety work?
Answer: Nullable types use a question mark and require safe handling before non-null operations.
Q4. What is structured concurrency?
Answer: Structured concurrency ties asynchronous tasks to a scope so cancellation and lifetime are predictable.
Q5. Why use data classes?
Answer: Data classes provide value-oriented equality, copying, destructuring, and readable representations.
Quiz

Which habit best supports Inheritance in Kotlin?