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.
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:
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.
A subclass names its superclass after a colon and calls the superclass constructor right there. Overriding members need the override keyword:
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.
Use super to run the parent implementation and add to it:
An override member is itself open by default, so subclasses of LoggingNotifier could override it again. Write final override fun send(...) to stop that.
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:
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.
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:
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.
| Goal | Kotlin syntax |
|---|---|
| Allow subclasses | open class Base |
| Allow overriding a member | open fun, open val |
| Override | override fun, override val |
| Forbid further overriding | final override fun |
| Call parent implementation | super.name() |
| Must implement in subclass | abstract fun |
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.
Kotlin classes are final by default. Add open to the superclass, or use abstract or sealed if that fits your design.
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.
No. A class has one superclass, but it can implement any number of interfaces.
Mark the overriding member final, as in final override fun send(...).
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.