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 class in Kotlin is a restricted class declared with the abstract keyword that cannot be instantiated directly. It serves as a structural template for subclasses, allowing the definition of both concrete members (with state and implementation) and abstract members (without implementation) that derived classes must resolve.

Core Technical Characteristics

  • Instantiation: Attempting to instantiate an abstract class directly results in a compilation error. It must be subclassed.
  • Implicitly Open: Abstract classes are implicitly open for extension. Applying the open modifier to the class itself is redundant.
  • Abstract Members: Properties and functions declared with the abstract keyword do not have an implementation or initial state. They are implicitly open and mandate an override in any concrete subclass.
  • Concrete Members: Functions and properties without the abstract modifier behave as they do in standard classes. They are implicitly final (cannot be overridden unless explicitly marked open) and can maintain state or provide default implementations.
  • Constructors: Abstract classes can define primary and secondary constructors, as well as init blocks. Subclasses are required to invoke the superclass constructor.

Syntax Visualization

// Abstract class with a primary constructor
abstract class BaseEntity(val id: String) {
    
    // Abstract property: No initializer allowed, must be overridden
    abstract val entityType: String

    // Concrete property: Maintains state, implicitly final
    val createdAt: Long = System.currentTimeMillis()

    // Abstract function: No body allowed, must be overridden
    abstract fun process()

    // Concrete function: Has implementation, implicitly final
    fun logStatus() {
        println("Entity $id of type $entityType created at $createdAt")
    }

    // Open concrete function: Has implementation, optionally overridden
    open fun validate(): Boolean {
        return id.isNotBlank()
    }
}

// Concrete subclass inheriting from the abstract class
class UserEntity(id: String) : BaseEntity(id) {
    
    // Mandatory override of abstract property
    override val entityType: String = "USER"

    // Mandatory override of abstract function
    override fun process() {
        println("Processing user $id")
    }
    
    // Optional override of open concrete function
    override fun validate(): Boolean {
        return super.validate() && id.startsWith("USR_")
    }
}

Advanced Mechanics

Overriding Open Members with Abstract An intermediate abstract class can override an open member from its superclass and declare it abstract. This forces all subsequent concrete subclasses down the hierarchy to provide a new implementation, effectively stripping the default implementation provided by the original superclass.
open class StandardComponent {
    open fun render() {
        println("Standard render")
    }
}

abstract class CustomComponent : StandardComponent() {
    // Strips the default implementation; subclasses MUST implement render()
    abstract override fun render()
}
Property Initialization Rules Abstract properties cannot be initialized or possess custom getters/setters within the abstract class. They strictly define the contract (name and type). If an abstract property is declared as a val, a subclass can override it as a var. However, an abstract var cannot be overridden as a val.
Master Kotlin with Deep Grasping Methodology!Learn More