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 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.
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).
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).
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.
open class StateManager {
    protected var stateCode: Int = 0
        private set // Setter is restricted to StateManager only; subclasses can only read
}
Master Kotlin with Deep Grasping Methodology!Learn More