In Swift, what does `final` prevent on a class versus on one member, and how do `static` and `class` type methods behave under override?
answer
- closes the type vs one member
- final class func
- static is already final
- class var must be computed
basics
~20 sfinal on a Swift class forbids subclassing; on a method, property or subscript it forbids overriding that member only. On a class, a static member cannot be overridden, while a class member can unless it is also marked final.
solid answer
~40 s`final class UploadService` cannot be subclassed ("inheritance from a final class"), and all its members are implicitly final. On a non-final class you can close single members with `final var`, `final func`, `final subscript` or `final class func`, and overriding one is an error such as "instance method overrides a 'final' instance method". For type members, `class func` is overridable and `static func` is not. On a class, `static` is effectively `class final`, which is why `static final` is rejected as "static declarations are already final". An overridable `class var` must be computed, since stored class properties are not supported. `final` closes the class to subclasses only: it does not make instances immutable or stop extensions.
code
swift · 15 linesclass APIService {
final func signedRequest(path: String) -> String { "signed:" + path }
class func defaultTimeout() -> Double { 30 }
static func userAgent() -> String { "App/1.0" }
final class func retryLimit() -> Int { 3 }
}
final class UploadService: APIService {
override class func defaultTimeout() -> Double { 120 } // OK
override func signedRequest(path: String) -> String { path } // error
override static func userAgent() -> String { "Upload" } // error
override class func retryLimit() -> Int { 5 } // error
}
class ChunkedUploadService: UploadService {} // errorgo deeper
Recall that final class stops subclassing and final func stops one override. Know which keyword makes a type method overridable.
Explain static as class final on classes, why an overridable class var must be computed, and the exact errors for overriding final or static members.
Use member-level final to protect invariants in a class that must stay subclassable. Note that only final classes can conform to Sendable, and violating that is an error in Swift 6 mode.
Decide which classes keep overridable members at all. Every non-final member is an extension point you must keep stable, so closing them is the cheaper default.
## `final` on a whole class Writing `final class UploadService` forbids subclassing. Any attempt is a compile-time error, "inheritance from a final class 'UploadService'". Because nothing can subclass it, every member of a final class is implicitly final as well. Writing `final` on a member inside a final class is redundant but legal. `final` does **not** freeze the class's state, make it a value type, or stop extensions. An extension can still add methods and computed properties to a final class. The keyword only closes the class to **subclasses**. ## `final` on one member On a class that stays open to subclassing, `final` can be applied member by member: `final var`, `final func`, `final subscript` and `final class func`. A subclass may still exist and add its own members, but overriding a final one is an error: - "instance method overrides a 'final' instance method" - "property overrides a 'final' property" - "class method overrides a 'final' class method" Members added in an extension can be marked `final` inside the extension too. `final` is only for classes and class members. On a struct member the compiler says "only classes and class members may be marked with 'final'", because value types have no subclasses to stop. ## `static` versus `class` type members Classes have two keywords for type-level members, and they differ exactly in overridability: | Declaration in a class | Overridable by a subclass? | Notes | |---|---|---| | `class func defaultTimeout()` | Yes | a subclass writes `override class func` | | `final class func retryLimit()` | No | "class method overrides a 'final' class method" | | `static func userAgent()` | No | "cannot override static method" | | `class var name: String { ... }` | Yes | must be computed | | `class var count = 0` | Not allowed | "class stored properties not supported in classes; did you mean 'static'?" | | `static let maxRetries = 3` | No | a constant type property is always implicitly final | So on a class, **`static` means `class final`**. Writing both is rejected with "static declarations are already final". Overridable type properties must be computed, because Swift supports stored type properties only as `static`. ## What `final` buys 1. **A closed contract.** A final member's behaviour is the behaviour you wrote. No subclass can change what `signedRequest(path:)` does, so the class's own invariants hold. 2. **Clearer review.** Reviewers do not have to ask which overrides exist in other files. 3. **Concurrency.** A class can conform to `Sendable` only if it is final. In Swift 5 mode a non-final class gets a warning, "this is an error in the Swift 6 language mode", and in Swift 6 mode it is an error. The details belong to the lesson on Sendable. 4. **Optimisation.** The compiler can call a final member directly. How dispatch works is covered in the material on method call mechanisms. ## Choosing between the two forms Class-level `final` is the stronger and simpler statement: nothing in the class can change. Member-level `final` fits a class that is meant to be subclassed but has a few members whose behaviour must stay fixed: - A **template method** such as `final func send(path:)` that runs fixed steps and calls overridable hooks. The steps cannot be reordered, only the hooks customised. - A **security or validation step**, such as `signedRequest(path:)`, that no subclass may skip. - A **type-level policy**, such as `final class func retryLimit()`, that subclasses read but cannot change. The two forms combine with the access rules: across a module boundary only `open` members can be overridden at all, so `final` matters most inside the module that owns the class. ## Mistakes to avoid - Thinking `final` makes a class immutable or behave like a struct. - Writing `static` on a class type method that subclasses are meant to customise. - Trying to declare a stored `class var`. - Marking members of a struct `final`.
- Can a subclass shadow a superclass's static method by declaring its own static method with the same name?No. Without `override` the compiler reports both the missing-override error and "cannot override static method", and with `override` the second error remains. If subclasses need their own version, the superclass declares it `class func`. If they must never change it, `static` is the right choice and the subclass picks a different name.
- Does marking a class final stop other files from extending it?No. `final` only forbids subclassing and overriding. Any file that can see the class can still add methods, computed properties and protocol conformances in an extension. Access control, not `final`, decides who can see and extend it.
- final makes a class's properties immutable like let
- A static method on a class can be overridden with override static
- You can declare a stored class var to share overridable state
- final can be applied to struct methods to stop overriding
- Marking a class final prevents extensions from adding methods