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 function in Kotlin is a function declaration that lacks an implementation body and is explicitly marked with the abstract keyword. It establishes a strict contract at the compiler level, compelling any concrete (non-abstract) subclass to provide a specific implementation for that function.

Syntax

abstract class BaseClass {
    // Abstract function declaration (no body)
    abstract fun processInput(input: String): Int
}

class ConcreteClass : BaseClass() {
    // Mandatory implementation using the 'override' keyword
    override fun processInput(input: String): Int {
        return input.length
    }
}

Technical Rules and Mechanics

  • Enclosing Scope: An abstract function can be declared inside an abstract class or an interface. Attempting to declare an abstract function inside a standard (concrete) class results in a compilation error. Note that functions declared inside an interface without a body are implicitly abstract; while you can explicitly write the abstract keyword on an interface function, it is redundant.
  • No Function Body: The declaration must terminate after the return type. It cannot be followed by a block body { ... } or an expression body = ....
  • Implicitly Open: Because abstract functions are designed specifically to be overridden, they are implicitly open. They cannot be marked with the final modifier.
  • Visibility Constraints: Abstract functions cannot be private, as they must be accessible to derived classes for implementation. They default to public but can be restricted to protected or internal.
  • Mandatory Overriding: Any concrete subclass inheriting from the abstract base class or interface must implement the abstract function using the override modifier.
  • Propagation to Subclasses: If a subclass inherits an abstract function but does not provide an implementation, that subclass must also be declared abstract. The implementation obligation is deferred down the inheritance tree.
abstract class IntermediateClass : BaseClass() {
    // Valid: No override provided, but the class is marked abstract.
    // The obligation to implement processInput() passes to the next subclass.
}
  • Abstracting Concrete Functions: An abstract function can override a concrete, open function inherited from a parent class. By using the abstract override modifiers, an abstract subclass discards the parent’s default implementation and forces concrete subclasses further down the hierarchy to provide their own implementation.
open class ParentClass {
    open fun executeTask() {
        println("Default implementation")
    }
}

abstract class ChildClass : ParentClass() {
    // Discards ParentClass's implementation, making it abstract again
    abstract override fun executeTask()
}

class GrandchildClass : ChildClass() {
    // Mandatory implementation forced by ChildClass
    override fun executeTask() {
        println("New mandatory implementation")
    }
}
Master Kotlin with Deep Grasping Methodology!Learn More