What is the final class in Swift?

Swift Class

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:

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:

  • Preventing Subclassing: If you have a class whose design or behavior should not be modified or extended, marking it as final ensures that its functionality remains intact and cannot be altered by subclasses.
  • Performance Optimization: The compiler can perform certain optimizations on final classes and methods because it knows they cannot be overridden.


Usage:

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.


Final Methods and Properties:


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 bottom line


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!


Share this article



Written By

Generic placeholder image

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

Related Articles


Deep Dive into Autorelease Pools in Swift

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

Read More »

Swift enum equatable: with or without associated values

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

Read More »

How to create Date Picker in SwiftUI

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

Read More »

Getting started with Swiftlint to enforce Swift style

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

Read More »

How to use Lottie animation in SwiftUI

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

Read More »

How to get index in Foreach in SwiftUI

In SwiftUI, the ForEach is used to iterate over a collection of data and create views dynamically based on that data....

2023-12-07 . 3 min read     SwiftUI ForEach

Read More »