What is the 'open' keyword in Swift?


In swift, we have three keywords - Open, Public, and Final keyword. All these three words have different properties that help us understand whether the code can be extended to another module or not making the code easy to reuse. We will learn about the keywords' properties in this article.

Example 

Here's an example of how the open keyword is used in a class definition

import Foundation
open class Person {
   var firstName: String?
   var lastName: String?
   var age: Int?
   var address: String?
}
class Student: Person {
   var school: String?
   var grade: Int?
   var score: Double?
}

In this example, a Person is defined as open, which means it can be subclassed. The student is a subclass of a Person class and is able to inherit its properties and methods.

If we change the open keyword to final

import Foundation
final class Person {
   var firstName: String?
   var lastName: String?
   var age: Int?
   var address: String?
}
class Student: Person {
   var school: String?
   var grade: Int?
   var score: Double?
}

The Person is now defined as final, which means it cannot be subclassed. This will cause a compile-time error in the Student.

It's worth noting that the default behavior for classes in Swift is final, so if you want a class to be able to be subclassed, you must explicitly use the open keyword.

You can use open keyword with structs and protocols as well, and you will get the same behavior as classes.

It is important to know the difference between open and final and the difference between open and public.

What is the difference between open and final?

The open access can be used with a class, struct, or protocol. When you use open, it indicates that you can make subclasses or extend by other classes, structs, or protocols within the same module or by other modules. It helps to make your code more flexible and reusable. This concept helps to achieve the concepts of object-oriented programming.

On the other hand, the final keyword indicates that a class, struct, or protocol cannot be extended or subclassed. When you want to secure your code and don’t want to provide flexibility to reuse the code, the final keyword is a good choice. This means if a class is final, you can not make a subclass of it.

Here is summarize the differences in both keywords −

Open vs Final in Swift

Keyword Accessibility Overridability
open Accessible Overridable
final Accessible Not Overridable

The final keyword ensures that the class, struct, or protocol is not changed by subclass or extension. Basically, this helps in improvement in code optimization. Also, the compiler does not need to take care of the extendibility of the class, struct, or protocol as they are final. Finally, it improves the overall performance of the application.

In short, open allows for extension and subclassing, and final prevents it.

Difference between open and public?

The open access can be used with a class, struct, or protocol. When you use open, it indicates that you can make subclasses or extend by other classes, structs, or protocols within the same module or by other modules. Your code gets more flexible and can be reused.

The public keyword has the property to indicate that other codes in the same module or other modules (That imports the same module) can access or use class, struct, or even protocol.

Here is summarize the differences in both keywords −

Open vs public in Swift

Keyword Accessibility Overridability Subclassing
open Accessible in any module Overridable Allowed
public Accessible in any module Not Overridable Disallowed

Conclusion

In this article, we found that we can use all three access for different purpose as follows −

Open Keyword − It can be accessed by any module, it is overridable and it allows subclassing.

Public Keyword − Any module can access it, but it is not overridable and it doesn’t allow subclassing at all.

Final Keyword − It cannot be extendable or even subclassed.

In case, you want to completely secure your code, the final keyword should be your choice.

Updated on: 28-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements