Sobes.tech
Junior

Is the open class final or non-final?

sobes.tech AI

Answer from AI

A class marked as open is non-final. The open keyword in Swift allows other modules (not just within the same module as public) to inherit from this class and override its methods and properties. A class without open or final is internal by default and can only be inherited within the same module. A class marked as final cannot be inherited at all.

Comparison table:

Keyword Inheritance (within the same module) Inheritance (in other modules) Overriding (within the same module) Overriding (in other modules) Default (without open/public/internal/fileprivate/private and final)
open Yes Yes Yes Yes No (default internal)
public Yes No Yes No No (default internal)
internal Yes No Yes No Yes
fileprivate No No Yes No No (default internal)
private No No Yes No No (default internal)
final No No No No No (can be used with open - which is illogical, or with other access levels)

Example:

// Class that can be inherited and overridden in other modules
open class OpenClass {
    open func openMethod() {
        // Implementation
    }
}

// Class that can be inherited and overridden only within the same module
public class PublicClass {
    public func publicMethod() {
        // Implementation
    }
}

// Class that cannot be inherited
final class FinalClass {
    func finalMethod() {
        // Implementation
    }
}
Is the open class final or non-final? — iOS - sobes.tech