61

What is the difference between final Class and Class?

final class A {

}

class B {    

}
Hamish
  • 78,605
  • 19
  • 187
  • 280
Mohammad Razipour
  • 3,643
  • 3
  • 29
  • 49
  • 1
    `A` can't be extended https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Inheritance.html – Héctor Sep 05 '17 at 07:58
  • 14
    @Héctor You mean inherited from; extension is an entirely different thing in Swift. – Hamish Sep 05 '17 at 08:56

7 Answers7

93

Classes marked with final can not be overridden.


Why should one care at all whether class can or can't be overridden?

There are two things to consider:

  1. As an API designer/developer, you might have a class in your Framework that can be abused or misused when subclassed. final prevents class to be subclassed—mission accomplished. You can also use final to mark methods, properties, and even subscripts of non-final classes. This will have same effect, but for particular part of the class.
  2. Marking class as final also tells Swift compiler that method should be called directly (static dispatch) rather than looking up a function from a method table (dynamic dispatch). This reduces function call overhead and gives you extra performance. You can read more on this on Swift Developer Blog.
jamix
  • 5,484
  • 5
  • 26
  • 35
totocaster
  • 6,243
  • 5
  • 37
  • 46
  • 20
    I want to add another (imo important) reason when to add final to a class: You as the developer simply have not thought about inheritance while designing the class. To show this to your colleagues (or the user of your class) add `final`. If inheritance will be necessary later on, the final modifier will remind you to think the refactoring through. – cornr Apr 07 '18 at 19:51
  • 4
    Great answer. Interesting how I did detect a performance boost in my whole app. I had all classes in my project declared without the `final` keyword, as I didn't subclass almost any of those, adding the `final` keyword really gave me a performance boost. – Ivan Cantarino Apr 24 '18 at 15:29
  • 5
    You don't need `final` on *internal* declarations when you use Whole Module Optimization, it's inferred (see the linked Swift Developer Blog post). – Marián Černý Aug 25 '18 at 12:33
91

Final is class modifier which prevents it from being inherited or being overridden. From apple 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 mark an entire class as final by writing the final modifier before the class keyword in its class definition (final class). Any attempt to subclass a final class is reported as a compile-time error.

luckyShubhra
  • 2,731
  • 1
  • 12
  • 19
22

Use final when you know that a declaration does not need to be overridden. The final keyword is a restriction on a class, method, or property that indicates that the declaration cannot be overridden. This allows the compiler to safely elide dynamic dispatch indirection.

Read more:

https://developer.apple.com/swift/blog/?id=27

Vlad Khambir
  • 4,313
  • 1
  • 17
  • 25
14

Final means that no one can inherit from this class.

iamirzhan
  • 914
  • 5
  • 16
10

Besides final declaration does not need to be overridden also has better performance than no use final, because final disable dynamic dispatch at runtime which save runtime overhead.

William Hu
  • 15,423
  • 11
  • 100
  • 121
8

Hoping I can boil down the other answers into this SSCCE that helped me:

          open class Open {}     // Anyone can see, anything can subclass
public         class Normal {}   // Anyone can see, internal can subclass
internal       class Internal {} // Internal can see, internal can subclass
public   final class Final {}    // Anyone can see, nothing can subclass

In your project/module:

class SubOpen:     Open {}     // OK
class SubNormal:   Normal {}   // OK
class SubInternal: Internal {} // OK
class SubFinal:    Final {}    // Error: Can't subclass

In some other project/module:

class SubOpen:     Open {}     // OK
class SubNormal:   Normal {}   // Error: Can't subclass
class SubInternal: Internal {} // Error: `Internal` type not found
class SubFinal:    Final {}    // Error: Can't subclass
Ky -
  • 30,724
  • 51
  • 192
  • 308
7

Other answers are already given enough understanding about final keyword. I want to explain by some example.

Consider below example without final Keyword.

class A {
   public var name: String
   var breed: String

    init(name: String, breed: String) {
        self.name = name
        self.breed = breed
    }
}
class B:A{
    override init(name: String, breed: String) {
        super.init(name: name, breed: breed)
    }
}

In above code it allow to overwrite the varible of it's super class. While Class with final keyword don't allow it. Refer below example.

final class A {
   public var name: String
   var breed: String

    init(name: String, breed: String) {
        self.name = name
        self.breed = breed
    }
}

class B:A{
    **//ERROR:inheritance from a final class 'A' class B**
    override init(name: String, breed: String) {
        super.init(name: name, breed: breed)
    }
}

Above code will give error inheritance from a final class A class B

Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65