> ## 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.

# Kotlin Override Property

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.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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).

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
open class BaseNode {
    open val parent: BaseNode? = null
}

class LeafNode : BaseNode() {
    // Legal: LeafNode is a subtype of BaseNode
    override val parent: LeafNode? = null 
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
