ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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
Theprotected modifier is placed before the fun keyword (and before open if the function is designed to be overridden).
Structural Constraints and Rules
1. Top-Level Declarations Theprotected 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.
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.
Master Kotlin with Deep Grasping Methodology!Learn More





