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
). Remember, anyone who subclasses your class can override your properties and perhaps also your methods, which offers them incredible power. If you do something they don't like, bam – they can just replace that. They might still call your original method as well as their replacement, but they might not.
This can be problematic: perhaps your class does something really important that mustn't be replaced, or perhaps you have clients on a support contract and you don't want them breaking the way your code works. www.hackingwithswift.comWhen inheriting, you can't change the property type. (Int -> Double, for example)