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.

A final class in Swift is a class declaration modified with the final keyword to explicitly prevent it from being inherited by any other class. Applying this modifier instructs the compiler to reject any attempt to subclass the entity, effectively sealing its inheritance hierarchy.

Architectural Mechanics

By default, Swift classes utilize dynamic dispatch. When a method or property is accessed, the runtime consults a virtual method table (v-table) to determine the correct implementation to invoke. This mechanism is what enables polymorphism and method overriding. When a class is marked final, the compiler guarantees that the class cannot be subclassed, meaning its methods and properties can never be overridden. Consequently, the Swift compiler optimizes the code by replacing dynamic dispatch with static (direct) dispatch. The compiler resolves the method calls at compile time, bypassing the v-table lookup entirely. This eliminates the runtime overhead associated with indirect calls and improves execution performance.

Access Control and Module Boundaries

In Swift, the final keyword interacts closely with access control mechanics. A class marked public is already effectively “final” outside of its defining module; it can be instantiated by external modules but cannot be subclassed across module boundaries unless it is explicitly marked open. Therefore, the final keyword is specifically required to seal a class within its own module, or to explicitly seal internal, fileprivate, or private classes.

Syntax and Compiler Enforcement

The final modifier precedes the class keyword. Access control modifiers can be placed between final and class (or before final), making declarations like final public class perfectly valid syntax.
final public class CoreEngine {
    var version: String = "1.0"
    
    func initialize() {
        // Initialization logic
    }
}
If another class attempts to inherit from a final class, the Swift compiler emits a fatal error during the compilation phase, preventing the code from building.
// Compiler Error: Inheritance from a final class 'CoreEngine'
class CustomEngine: CoreEngine {
    var customProperty: Int = 0
}

Member-Level Finality

While marking an entire class as final implicitly finalizes all of its members, the final modifier can also be applied granularly to specific methods, properties, or subscripts within a non-final class. This prevents the overriding of those specific members while still allowing the class itself to be subclassed.
class BaseComponent {
    // Dynamically dispatched; can be overridden by subclasses
    func render() {
        print("Rendering base component")
    }
    
    // Statically dispatched; cannot be overridden
    final func calculateBounds() {
        print("Calculating fixed bounds")
    }
}

class CustomComponent: BaseComponent {
    // Valid override
    override func render() {
        print("Rendering custom component")
    }
    
    // Compiler Error: Instance method overrides a 'final' instance method
    override func calculateBounds() {
        print("Attempting to override fixed bounds")
    }
}
Master Swift with Deep Grasping Methodology!Learn More