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

A `protected` function in Kotlin is a class-member function restricted by the `protected` visibility modifier, making it accessible only within the exact class that declares it and any of its derived subclasses. Unlike Java, Kotlin's `protected` modifier does not grant package-private visibility; access is strictly bound to the inheritance hierarchy regardless of the package structure.

## Syntax and Access Resolution

The `protected` modifier is placed before the `fun` keyword (and before `open` if the function is designed to be overridden).

```kotlin theme={"dark"}
open class BaseClass {
    protected open fun executeInternalLogic() {
        // Function body
    }
}

class DerivedClass : BaseClass() {
    fun invokeLogic() {
        executeInternalLogic() // Valid: Accessed from within a subclass
    }
}

class UnrelatedClass {
    fun attemptAccess(base: BaseClass) {
        // base.executeInternalLogic() // Compilation Error: Cannot access 'executeInternalLogic'
    }
}
```

## Structural Constraints and Rules

**1. Top-Level Declarations**
The `protected` modifier cannot be applied to top-level functions (functions declared directly inside a file, outside of any class). It is exclusively a class-member modifier. Attempting to declare a top-level `protected` function results in a compilation error.

**2. Interface Declarations**
The `protected` visibility modifier is strictly prohibited inside interfaces. While interface members can be declared as `public` (the default), `private`, or `internal`, attempting to declare a `protected` function in an interface will immediately result in a compilation error (`Modifier 'protected' is not applicable inside 'interface'`).

**3. Extension Functions**
Extension functions do not inherit the visibility scope of the class they extend. An extension function cannot access `protected` functions of its receiver type, as extensions are resolved statically outside the class hierarchy.

```kotlin theme={"dark"}
open class Target {
    protected fun hiddenFunction() {}
}

fun Target.extensionFunction() {
    // hiddenFunction() // Compilation Error: Cannot access 'hiddenFunction'
}
```

**4. Overriding and Visibility Widening**
When a subclass overrides a `protected` function, the overriding function implicitly retains the `protected` visibility modifier. However, Kotlin allows visibility widening. A subclass can elevate the visibility of an overridden `protected` function to `public`.

It cannot restrict the visibility further (e.g., to `private`), nor can it change the visibility to `internal`. In Kotlin, `protected` and `internal` are orthogonal (incomparable) visibility modifiers. Overriding a `protected` member with `internal` is strictly prohibited because it would restrict access for subclasses located in other modules. Attempting to do so results in a compilation error.

```kotlin theme={"dark"}
open class Parent {
    protected open fun processData() {}
}

class Child : Parent() {
    // Valid: Visibility widened from protected to public
    public override fun processData() {} 
}

class InvalidInternalChild : Parent() {
    // Invalid: Cannot weaken access privilege 'protected' to 'internal'
    // internal override fun processData() {} 
}

class InvalidPrivateChild : Parent() {
    // Invalid: Cannot weaken access privilege 'protected' to 'private'
    // private override fun processData() {} 
}
```

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