Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

In Kotlin, overriding a property allows a subclass to redefine the accessors (getters and setters) of a property declared in a superclass or interface. Because Kotlin classes and members are final by default, the base property must be explicitly marked with the open modifier, and the derived property must be marked with the override modifier. Overriding a property strictly overrides its accessors; it does not override or skip the base class initialization logic, which still executes when the class is instantiated.

Basic Syntax

To override a property, the derived class must declare a property with the same name and a compatible type, prefixed with the override keyword. When an overridden property is initialized in the subclass, it creates a new backing field and a new getter in the subclass that reads from this new field.
open class Base {
    open val identifier: String = "BaseID"
}

class Derived : Base() {
    override val identifier: String = "DerivedID"
}

Primary Constructor Overriding

Properties can be overridden directly within the primary constructor of the derived class. This is a concise syntax for declaring and initializing overridden properties.
interface Polygon {
    val vertexCount: Int
}

class Rectangle(override val vertexCount: Int = 4) : Polygon

Mutability Rules (val vs var)

Kotlin enforces strict rules regarding property mutability during inheritance, based on the underlying accessors generated by the compiler:
  1. Overriding val with var is permitted: A val property declares only a getter. Overriding it with a var provides a new getter and a new setter in the derived class.
  2. Overriding var with val is prohibited: A var property requires both a getter and a setter. Overriding it with a val would illegally remove the setter contract defined by the superclass.
open class SuperClass {
    open val readOnlyProperty: Int = 0
    open var mutableProperty: Int = 0
}

class SubClass : SuperClass() {
    // Legal: Provides a new getter and a new setter
    override var readOnlyProperty: Int = 1 
    
    // ILLEGAL: Cannot reduce mutability of a var by removing the setter
    // override val mutableProperty: Int = 1 
}

Overriding Accessors

Instead of assigning a backing field value directly, you can override the custom getter (and setter, if applicable) of the property. You can also access the superclass implementation using the super keyword.
open class BaseEntity {
    open val typeName: String = "Entity"
}

class UserEntity : BaseEntity() {
    override val typeName: String
        get() = "User${super.typeName}"
}

Overriding with Property Delegation

An overridden property can be implemented using Kotlin property delegates (such as lazy, observable, or custom delegates).
open class Configuration {
    open val timeoutMs: Int = 1000
}

class NetworkConfiguration : Configuration() {
    override val timeoutMs: Int by lazy { 
        5000 
    }
}

Type Covariance in Overrides

When overriding a property, the type of the overriding property must be a subtype of the overridden property’s type. This covariance applies to read-only (val) properties.
open class BaseNode {
    open val parent: BaseNode? = null
}

class LeafNode : BaseNode() {
    // Legal: LeafNode is a subtype of BaseNode
    override val parent: LeafNode? = null 
}
Master Kotlin with Deep Grasping Methodology!Learn More