Posted on 27 May 2023 . 1 min read
In Swift, the final keyword is used to indicate that a class, method, or property cannot be overridden or subclassed. When you declare a class as final, it means that it's the end of the inheritance chain for that class, and it cannot be subclassed further.
final class MyClass {
// Class definition
}
By marking MyClass as final, you're explicitly stating that no other class can inherit from it. This can be beneficial in several scenarios:
final class Vehicle {
// ...
}
// This will produce an error, as 'Car' cannot inherit from a 'final' class 'Vehicle'
class Car: Vehicle {
// ...
}
If you mark class final, it means that it's the end of the inheritance chain for that class, and it cannot be subclassed further. If you try to implement inheritance it will produce an error, as Carcannot inherit from a finalclass Vehicle.
In addition to classes, you can also mark individual methods and properties as final to prevent overriding:
class Parent {
final func someMethod() {
// This method cannot be overridden by subclasses
}
final var someProperty: Int {
return 10
}
}
class Child: Parent {
// This will produce an error as 'someMethod()' and 'someProperty' are final
// and cannot be overridden
}
By using final for methods and properties within a class, you ensure that these elements cannot be modified or extended by subclasses.
The use of final provides a way to explicitly define parts of your code that should not be overridden or extended, offering a level of control over the inheritance and customization of classes, methods, and properties in Swift.
Don’t hesitate to contact me if you have any questions or queries. Follow me on twitter @gurjitpt for any updates.
Thanks!
Written By
Gurjit Singh
I’m Computer Science graduate and an iOS Engineer who writes about Swift and iOS development. Follow me for more updates:
Discover articles by topics
SwiftUI Class Struct Networking XCode NSCache Enum Optionals Property Observers Closures Guard Reviews StoreKit App Store Algorithms Testing Operators Protocol Extensions Weak Unowned SwiftData WWDC23 GCD API Admob SwiftLint Lottie Foreach Objective-C UIKit NavigationSplitView
With the introduction of SwiftUI, Apple has provided developers with a modern way to build user interfaces across all Apple platforms....
2024-07-09 . 3 min read UIHostingController
In the realm of software development, memory management plays a crucial role in ensuring the efficient allocation and deallocation of memory...
2024-01-28 . 4 min read Swift Autorelease
Swift enums provide a powerful way to model a set of related values. Enums can be equipped with associated values, allowing them to represen...
2024-01-24 . 3 min read Swift Enums
Use a DatePicker when creating a view that enables users to choose both a calendar date and, if needed, a specific time.In SwiftUI, you can ...
2024-01-16 . 2 min read SwiftUI DatePicker
SwiftLint is a tool that ensures Swift code adheres to defined style guidelines. It automates code review by identifying and suggesting impr...
2023-12-29 . 4 min read Swift SwiftLint
Lottie is a fantastic tool for incorporating high-quality animations into your SwiftUI projects. There are several ways to add Lottie to pro...
2023-12-13 . 2 min read SwiftUI Lottie