Sobes.tech
Junior — Middle

Milline on erinevus klassi deklaratsiooni vahel, kasutades võtmesõna open, ja tavalise klassi määratluse vahel ilma selle modifikaatorita?

sobes.tech AI

Vastus AI-lt

In the Swift language, by default, all classes are closed for inheritance (final). This means that without the open or public modifier, a class cannot be inherited outside the module where it is declared.

  • A class declared with the open keyword can be inherited and overridden in any other module (for example, in another framework or application).
  • A class without the open modifier (usually public or no modifier) cannot be inherited outside the current module.

Therefore, open is the most "open" access level for classes and their methods, allowing functionality to be extended in other modules.

Example:

open class OpenClass {
    open func doSomething() {}
}

public class PublicClass {
    public func doSomething() {}
}

// Inheritance from OpenClass from another module is possible
// Inheritance from PublicClass from another module is not possible