Prevent subclasses from overriding inherited functions in Swift

Is there a way to prevent subclassing from overriding inherited functions in Swift?

+3


source to share


1 answer


Look at the keyword final

.

According to the documentation,



You can prevent a method, property, or index from being overridden by marking it as final. Do this by writing a modifier final

to the keywords the introduction of a method, property, or indexes (eg, final var

, final func

, final class func

and final subscript

).

Any attempt to override a final method, property, or index in a subclass is reported as a compile-time error. Methods, properties, or indexes that you add to a class in an extension can also be marked as final in the extension definition.

You can find more information at the bottom of the Inheritance> section of the Swift Language manual (see the section "Preventing Overrides").

+14


source







All Articles