Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How does inheritance work in Kotlin with open and override?

Android
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 25, 2026
6 min read

Kotlin inheritance lets a class reuse and extend another class, but every class is final by default. To allow a class to be extended you mark it open, and to let a subclass change a member you mark that member open and then use override in the subclass. This closed-by-default design is the key difference from Java and avoids accidental breakage in subclasses.

This guide covers kotlin inheritance with open and override, calling the superclass, constructors in subclasses and safe type checks.

Table of Contents

  • Why classes are final by default
  • Extending a class with open and override
  • Calling the superclass with super
  • Constructors and properties in subclasses
  • Abstract classes, is checks and smart casts
  • Inheritance rules at a glance
  • When composition is better
  • Frequently Asked Questions
    • Why do I get "This type is final, so it cannot be inherited from"?
    • What is the difference between open and abstract?
    • Can a Kotlin class extend more than one class?
    • How do I stop a member from being overridden further?
  • Summary
  • Related Articles

Why classes are final by default

In Java, any non-final class can be extended, which makes it easy to break a base class by changing it. Kotlin follows the idea that you should design for inheritance deliberately or forbid it. This works:

kotlin

Remove open and class Dog : Animal("Rex") will not compile, with an error that the type is final. The same applies to members: a function in an open class is still final unless it is marked open too. In the example above describe() is final, so no subclass can change it.

Extending a class with open and override

A subclass names its superclass after a colon and calls the superclass constructor right there. Overriding members need the override keyword:

kotlin
text

Each call to sound() runs the version that belongs to the real object, not to the declared type Animal. This is dynamic dispatch, the core of polymorphism.

Calling the superclass with super

Use super to run the parent implementation and add to it:

kotlin
text

An override member is itself open by default, so subclasses of LoggingNotifier could override it again. Write final override fun send(...) to stop that.

Constructors and properties in subclasses

If the superclass has a primary constructor, the subclass must call it. Without a primary constructor, each secondary constructor calls super(...). You can also override properties, as long as the base property is open:

kotlin
text

A val can be overridden with a var, but not the other way round, since a var promises a setter that a val cannot give. Avoid reading an open property inside a base class constructor. The subclass has not been initialized yet, so you can see a default value like 0 or null.

Abstract classes, is checks and smart casts

An abstract class is open implicitly and can leave members without a body. You will meet this when designing a shared base. Checking the real type uses is, and Kotlin then casts automatically inside the branch:

kotlin

For closed hierarchies where you list every subtype, a sealed class lets the compiler check that a when covers all cases. Use as? for a safe cast that returns null instead of throwing ClassCastException.

Inheritance rules at a glance

GoalKotlin syntax
Allow subclassesopen class Base
Allow overriding a memberopen fun, open val
Overrideoverride fun, override val
Forbid further overridingfinal override fun
Call parent implementationsuper.name()
Must implement in subclassabstract fun

When composition is better

Inheritance models "is a" relationships, and deep trees become fragile. Many Kotlin and Android codebases prefer interfaces plus delegation with by, or small helper classes passed through the constructor. Reach for inheritance when subclasses truly share state and behaviour and the base class is documented for extension.

Frequently Asked Questions

Why do I get "This type is final, so it cannot be inherited from"?

Kotlin classes are final by default. Add open to the superclass, or use abstract or sealed if that fits your design.

What is the difference between open and abstract?

An open member has an implementation that subclasses may replace. An abstract member has no implementation and subclasses must provide one. Abstract classes cannot be instantiated.

Can a Kotlin class extend more than one class?

No. A class has one superclass, but it can implement any number of interfaces.

How do I stop a member from being overridden further?

Mark the overriding member final, as in final override fun send(...).

Summary

Kotlin classes and members are final unless you opt in with open. Subclasses extend with : Parent(args) and change behaviour with override, calling the parent through super when needed. Use final override, abstract and sealed to control the design, and prefer interfaces and delegation when a deep hierarchy would be fragile.

Related Articles

  • How do interfaces and abstract classes work in Kotlin?
  • How do List, Set and Map collections work in Kotlin?
  • Learn Android: Complete Study Roadmap from Beginner to Advanced
Share