What Is a Final Class in Swift

What Is a Final Class in Swift

You can use a final class in Swift to disable the inheritance of the class.

For example, let’s create a class Student and make it a final class to disallow inheritance:

final class Student {
    var name: String

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

If you now try to subclass this class, you are going to see a compile-time error:

class Researcher: Student { }

Output:

/main.swift:9:7: error: inheritance from a final class 'Student'
class Researcher: Student { }
      ^

This error says it is not possible to inherit from a final class.

If you try to subclass any final class in Swift, this is what you should expect to see.

Make Parts of the Class “Final” in Swift

If you mark the class final, everything inside that class becomes final.

But you can also make specific parts of a class final, that is, the properties and methods of it.

To do this, add the final keyword in front of the declaration/definition.

For instance, let’s create a class Student whose showName method should not be overrideable. To do this, mark the method final:

class Student {
    var name: String

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

    final func showName(){
      print(self.name)
    }
}

Now it is possible to inherit the Student class, but overriding the showName method is not possible:

class Researcher: Student {
  override func showName(){
    print(self.name)
  }
}

The output is an error that says this method is not overridable:

/main.swift:14:17: error: instance method overrides a 'final' instance method
  override func showName(){
                ^

When Use Final Classes in Swift

Now you know how a final class or final properties and methods work in Swift.

But when is it actually useful?

Use a final class when you don’t want other developers to inherit from the class.

As you know, anyone who subclasses a class can override the properties and methods that belong to it. If the class does something that the developer does not like, they can just replace it with what they want.

But if your class does something really important, you may want to disable inheritance. This prevents developers break the way your code should work.

Here are some situations where you can use final classes in Swift:

  1. Misusage. If you have a class that can easily be misused by a subclass, make the class final to prevent inheritance altogether.
  2. Unclear consequences. If you have not thought about the consequences of inheritance when designing the class, mark the class final. This highlights that inheritance should be thought thoroughly before making it possible.
  3. Performance. Mark a class final to tell the Swift compiler that a method should be called directly instead of looking it up from a method table. Read more here.

Conclusion

A final class in Swift prevents the class from being inherited.

To write a final class, type the keyword final in front of the class definition.

You can also mark methods or properties final to prevent them from being overridden in the subclass.

Thanks for reading. Happy coding!

Further Reading

50 Swift Interview Questions

iOS Development Tips

Share on facebook
Share on twitter
Share on linkedin

Leave a Comment

Your email address will not be published.