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.

The final modifier in Swift is a declaration attribute applied to a class property to explicitly prevent any subclass from overriding its implementation, including its getter, setter, or property observers. Under the hood, the final modifier alters how the Swift runtime handles property resolution. By default, non-private properties in Swift classes utilize dynamic dispatch, requiring the runtime to consult a virtual method table (vtable) to resolve the correct property implementation. Applying the final modifier instructs the compiler to use static (direct) dispatch. This eliminates the vtable lookup overhead, allowing the compiler to inline the property access and optimize execution speed.

Syntax and Application

The final keyword is placed immediately before the property declaration. It is applied to variable properties (var), encompassing both stored and computed properties. While the compiler syntactically permits placing final before a constant (let) declaration, doing so is functionally redundant. Swift strictly prohibits overriding constant properties by definition, meaning let properties are inherently immune to subclass overrides without requiring the final modifier.
class BaseClass {
    // Final stored property
    final var identifier: String = "Base"
    
    // Final computed property
    final var description: String {
        return "Identifier is \(identifier)"
    }
    
    // Final property with observers
    final var state: Int = 0 {
        didSet {
            print("State changed to \(state)")
        }
    }
}

Compiler Enforcement

The Swift compiler enforces the final restriction at compile time. Any attempt to use the override keyword on a final property in a subclass—even with syntactically valid override patterns like adding property observers or providing custom getters/setters—will immediately halt compilation and throw an error.
class SubClass: BaseClass {
    // ❌ Compiler Error: Var overrides a 'final' var
    override var identifier: String {
        get { return "Sub" }
        set { }
    }
    
    // ❌ Compiler Error: Var overrides a 'final' var
    override var state: Int {
        didSet {
            print("SubClass state changed")
        }
    }
}

Scope and Limitations

  • Class-Bound: The final modifier is only valid within class types. Because struct and enum types do not support inheritance, applying final to their properties is syntactically invalid and will result in a compiler error.
  • Protocol Exclusions: You cannot apply final to property requirements within a protocol declaration, as protocols only define signatures, not implementations.
  • Class-Level Redundancy: If an entire class is declared as final (e.g., final class MyClass), the class itself cannot be subclassed. Consequently, all properties within that class are implicitly final, making the addition of the final keyword to individual properties redundant.
  • Extensions: Properties added to a class via an extension are implicitly final because Swift does not allow overriding declarations from extensions in subclasses (unless exposed to Objective-C via @objc).
Master Swift with Deep Grasping Methodology!Learn More