Is there a way to prevent subclasses from overriding inherited functions in Swift?
Take a look at the final
keyword.
According to the documentation,
You can prevent a method, property, or subscript from being overridden by marking it as final. Do this by writing the
final
modifier before the method, property, or subscript’s introducer keyword (such asfinal var
,final func
,final class func
, andfinal subscript
).Any attempt to override a final method, property, or subscript in a subclass is reported as a compile-time error. Methods, properties, or subscripts that you add to a class in an extension can also be marked as final within the extension’s definition.
You can find more information at the bottom of the Inheritance section of the Swift Language Guide (look for the "Preventing Overrides" subsection).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With