Skip to main content
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.

Syntax and Declaration

Abstract properties are declared using the abstract modifier followed by val (read-only) or var (mutable).
Because they are abstract, these properties cannot be initialized in the declaring class, nor can they define custom 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 the override modifier. This can be achieved in three ways:
  1. Primary Constructor Parameter: Declaring the overridden property directly in the subclass constructor.
  2. Direct Initialization: Assigning a default value in the class body, which generates a backing field.
  3. Custom Accessors: Providing custom get() (and set() for var) implementations without allocating a backing field.

Mutability Constraints

Kotlin enforces specific rules regarding property mutability during inheritance:
  • An abstract val can be overridden as either a val or a var. This is permitted because a val only guarantees the existence of a getter; overriding it as a var simply adds a setter, widening the property’s capabilities.
  • An abstract var must strictly be overridden as a var. A subclass cannot restrict a mutable contract to a read-only state.

Interfaces vs. Abstract Classes

While the abstract 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 field identifier is completely inaccessible within the declaration of an abstract property.
  • Implicitly Open: Abstract properties are inherently open. Applying the open modifier alongside abstract is 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.
Tired of Poor Kotlin Skills? Fix That With Deep Grasping!Learn More