> ## 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 Protected Property

A `protected` property in Kotlin is a class-member variable whose visibility is strictly confined to the declaring class and its direct or indirect subclasses. Unlike Java, Kotlin's `protected` modifier does not grant package-level access; visibility is exclusively bound to the inheritance hierarchy.

## Syntax and Declaration

The `protected` modifier is placed directly before the `val` or `var` keyword. It is only valid for class members and cannot be applied to top-level properties declared directly within a file.

```kotlin theme={"dark"}
open class BaseClass {
    protected val readOnlyProperty: String = "Base"
    protected var mutableProperty: Int = 0
}

class SubClass : BaseClass() {
    fun accessProperties() {
        // Accessible: SubClass inherits from BaseClass
        println(readOnlyProperty) 
        mutableProperty = 1
    }
}

fun externalFunction() {
    val instance = BaseClass()
    // Compilation Error: Cannot access 'readOnlyProperty': it is protected in 'BaseClass'
    // instance.readOnlyProperty 
}
```

## Visibility Rules and Receiver Constraints

* **Receiver Type Restriction:** When accessing a `protected` property from within a subclass, the receiver type must be the subclass itself or a subtype of it. A subclass cannot access a `protected` property if the receiver is explicitly typed as the base class. However, a subclass *can* access the `protected` property of *another* instance, provided that instance is typed as the subclass (or a subtype).

```kotlin theme={"dark"}
open class Base {
    protected val protectedProp: Int = 42
}

class Sub : Base() {
    fun testAccess(baseRef: Base, subRef: Sub) {
        // Valid: Implicit 'this' receiver is typed as Sub
        println(protectedProp) 
        
        // Valid: Explicit receiver is typed as Sub
        println(subRef.protectedProp) 
        
        // Compilation Error: Receiver is typed as Base, not Sub
        // println(baseRef.protectedProp) 
    }
}
```

* **Extension Functions:** Extension functions declared outside the class or its subclasses cannot access `protected` properties. They are treated as external callers.
* **Companion Objects:** A companion object is treated as an insider to its enclosing class and has full access to the class's `protected` properties. However, the `protected` modifier cannot be applied to properties declared *inside* a companion object (or any `object` declaration). Because objects are implicitly final and cannot be subclassed, attempting to do so yields a compiler error (`Modifier 'protected' is not applicable inside 'object'`).

## Overriding `protected` Properties

When a `protected open` property is overridden in a subclass, the overriding property retains the `protected` visibility by default. Kotlin permits widening the visibility modifier during an override (e.g., from `protected` to `public`), but strictly forbids narrowing it (e.g., from `protected` to `private`).

```kotlin theme={"dark"}
open class Configuration {
    protected open val timeout: Int = 3000
}

class PublicConfiguration : Configuration() {
    // Legal: Widening visibility to public
    public override val timeout: Int = 5000 
}

class RestrictedConfiguration : Configuration() {
    // Compilation Error: Cannot weaken access privilege 'protected' to 'private'
    // private override val timeout: Int = 1000 
}
```

## Getter and Setter Visibility

The visibility of a property's getter strictly matches the visibility of the property itself and cannot be modified. The setter of a `protected var`, however, can be assigned a more restrictive visibility modifier (`private`), but it cannot be widened beyond `protected`.

```kotlin theme={"dark"}
open class StateManager {
    protected var stateCode: Int = 0
        private set // Setter is restricted to StateManager only; subclasses can only read
}
```

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