Junior — Middle
What consequences will arise if a class is declared with the final modifier?
sobes.tech AI
Answer from AI
Declaring a class with the final modifier in Swift means that this class cannot be inherited. This prevents subclasses from being created from this class, which can improve code safety and performance, as the compiler can optimize method calls.
Example:
final class MyClass {
func doSomething() {
print("Doing something")
}
}
// Attempting to inherit will cause a compile-time error
// class SubClass: MyClass {} // Error