An abstract property in Kotlin is a property declared within an abstract class or interface that lacks an initial value, a backing field, and custom accessor implementations. It establishes a strict contract, mandating that any concrete subclass must override the property to provide its state or accessor logic.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.
Syntax and Declaration
Abstract properties are declared using theabstract modifier followed by val (read-only) or var (mutable).
get() or set() blocks. Attempting to provide a backing field or accessor logic to an abstract property will result in a compilation error.
Implementation Rules
When a concrete subclass inherits an abstract property, it must provide an implementation using theoverride modifier. This can be achieved in three ways:
- Primary Constructor Parameter: Declaring the overridden property directly in the subclass constructor.
- Direct Initialization: Assigning a default value in the class body, which generates a backing field.
- Custom Accessors: Providing custom
get()(andset()forvar) implementations without allocating a backing field.
Mutability Constraints
Kotlin enforces specific rules regarding property mutability during inheritance:- An
abstract valcan be overridden as either avalor avar. This is permitted because avalonly guarantees the existence of a getter; overriding it as avarsimply adds a setter, widening the property’s capabilities. - An
abstract varmust strictly be overridden as avar. A subclass cannot restrict a mutable contract to a read-only state.
Interfaces vs. Abstract Classes
While theabstract keyword is mandatory for abstract properties inside an abstract class, properties declared inside an interface are implicitly abstract if they do not define custom accessors.
Technical Restrictions
- No Backing Fields: The
fieldidentifier is completely inaccessible within the declaration of an abstract property. - Implicitly Open: Abstract properties are inherently
open. Applying theopenmodifier alongsideabstractis redundant and generates a compiler warning. - Delegation: Abstract properties cannot be delegated (e.g.,
abstract val prop: String by lazy { ... }is invalid) because delegation requires an implementation.
Master Kotlin with Deep Grasping Methodology!Learn More





