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.

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).
abstract class BaseEntity {
    abstract val identifier: String
    abstract var counter: Int
}
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.
class ConcreteEntity(
    override val identifier: String // 1. Implemented via primary constructor
) : BaseEntity() {

    // 2. Implemented via direct initialization (creates a backing field)
    override var counter: Int = 0 
    
    // 3. Alternatively, implemented via custom accessors:
    /*
    override var counter: Int
        get() = 42
        set(value) { println(value) }
    */
}

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.
interface Configuration {
    val theme: String // Implicitly abstract; no 'abstract' keyword needed
}

abstract class BaseConfiguration : Configuration {
    abstract val version: Int // Explicitly abstract; 'abstract' keyword required
}

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.
Master Kotlin with Deep Grasping Methodology!Learn More