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 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
Theprotected 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.
Visibility Rules and Receiver Constraints
- Receiver Type Restriction: When accessing a
protectedproperty from within a subclass, the receiver type must be the subclass itself or a subtype of it. A subclass cannot access aprotectedproperty if the receiver is explicitly typed as the base class. However, a subclass can access theprotectedproperty of another instance, provided that instance is typed as the subclass (or a subtype).
- Extension Functions: Extension functions declared outside the class or its subclasses cannot access
protectedproperties. 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
protectedproperties. However, theprotectedmodifier cannot be applied to properties declared inside a companion object (or anyobjectdeclaration). 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).
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 aprotected var, however, can be assigned a more restrictive visibility modifier (private), but it cannot be widened beyond protected.
Master Kotlin with Deep Grasping Methodology!Learn More





