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.

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).
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.
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.
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() {} 
}
Master Kotlin with Deep Grasping Methodology!Learn More