Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent subclasses from overriding inherited functions in Swift

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

like image 493
crvo84 Avatar asked Jul 15 '15 06:07

crvo84


1 Answers

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 as final var, final func, final class func, and final 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).

like image 129
ndmeiri Avatar answered Oct 18 '22 22:10

ndmeiri