한국어 | English | 日本語
Senior Web Application Developer (8.8+ years)
Tech & Dev
engineering
Focusing on web frontend and backend development

Understanding the `open` Keyword in Swift 4+ Syntax

Inheritance is a cornerstone of object-oriented programming, but its misuse can lead to the 'Fragile Base Class' problem, where changes in a base class break its subclasses. This article explores how modern languages like Swift grammatically enforce solutions to this issue.
This article analyzes the access control mechanisms in Swift and Kotlin, which by default make all classes non-inheritable (final) to prevent undesirable side effects of inheritance. Specifically, it explains the usage of the `open` keyword, which allows fine-grained control over inheritance at both class and method levels, comparing it with Java's approach.

Access Modifier

In programming languages, taking Java as an example, public, private, and protected are used to control the access scope of fields, functions, and classes. Inheritance-related keywords like final, open, override, and abstract are also included under the umbrella of ‘access modifiers’. (Access modifiers are sometimes referred to as Visibility Modifiers.)

Fragile Base Class

The Fragile Base Class problem is an issue arising from inheritance. It refers to a situation where changes made to a base class cause its subclasses to break, assuming there’s a base class and subclasses inheriting from it. If rules are not explicitly defined for which methods can be overridden and how when a base class is inherited by subclasses, subclasses might override any method in an unintended manner. Furthermore, if the purpose of a base class method changes, the overridden methods in subclasses may become unexpectedly divergent from the original intent, meaning changes to a base class impact all its inheriting subclasses. This vulnerability is why it’s called a Fragile Base Class.

Early object-oriented programming languages like Java, C#, and C++ made inheritance easy by default; if no access modifier is explicitly specified, all base classes are inheritable. However, to prevent the Fragile Base Class problem, software architecture and design patterns recommend against inheriting all base classes, as stated in ‘Effective Java’: “Design and document for inheritance, or forbid it.”

Limitations and problems discovered while using early object-oriented programming are sometimes addressed through continuous language updates, but newer languages often adopt these good patterns as their core features. Modern object-oriented programming languages like Kotlin and Swift are among them. While jokingly referred to as ‘hybrid’ or ‘mishmash’ languages (Korean: 짬뽕, meaning ‘mixed’), this effectively means they have the advantage of enforcing these patterns grammatically.

Swift and Kotlin: Non-Inheritable by Default, Unlike Java

While Java, an early programming language prone to the Fragile Base Class problem, makes all base classes inheritable by default, Kotlin and Swift address this issue by making all base classes non-inheritable (final) by default. Consequently, both Swift’s class and struct types are non-inheritable by default.

Additionally, in Java, variables, classes, and functions are package-private by default if no access modifier is specified. In contrast, in Kotlin and Swift, if no access modifier is specified, they are declared as public, making them usable from anywhere. However, public members are final by default, meaning they are non-inheritable.

open class User {
    open func login() { }
    public func playGame() { }
    public init() { }
}

In both Kotlin and Swift, to enable inheritance, the open keyword must be added to the class, function, and even variables. The advantage of using open for functions and variables is that it allows easy management of what is ‘inheritable’ and ‘non-inheritable’ at a granular level, rather than just at the class level.


As observed, Java’s approach to inheritance is completely opposite to that of Kotlin and Swift. Java allows public classes and functions to be inheritable by default, using private or protected access modifiers to restrict inheritance. In contrast, Kotlin and Swift default to making everything non-inheritable for developers, and then explicitly require open to be specified for additional inheritance, thereby preventing incorrect inheritance. This is why, when initially using open in Swift, it might feel similar to abstract in Java, as it dictates the inheritable status of functions and variables.


Understanding the `open` Keyword in Swift 4+ Syntax
Author
Aaron
Posted on
Licensed Under
CC BY-NC-SA 4.0
CC BY-NC-SA 4.0
More in this category
Recent posts
The Erosion of Conversational Muscle and Communication Styles by LLM Filters
In an era where LLM tools, which filter out conversational impoliteness and deliver refined responses, have become commonplace, are we truly engaging in more thoughtful conversations? This article examines the phenomenon of conversational ability, which should be honed through countless failures in real-time communication, degenerating due to reliance on external tools. It further explores the potential societal anxieties and shifts in generational behavioral patterns that this trend may bring.
Optimal Timing and Strategy for Salary Negotiation with Senior Candidates
Salary negotiation is more than just an exchange of figures; it's a strategic dance of psychological timing. This analysis explores why engaging in a gradual negotiation process from the initial stages of recruitment, rather than waiting until after a final offer (when candidates tend to adopt a more calculative stance), proves more efficient for companies and fosters a more honest sharing of resources.
The Limits of the Rule of Law and Human Diversity
The belief that all human actions can be regulated by a single legal system may be an act of hubris. This article offers a sharp analysis of the paradox of the rule of law faced by humanity, which, having escaped the hierarchical controls of the Middle Ages, has now embraced infinite modern freedom. It further examines the deepening social coercion and the demonization of others that arise under the guise of diversity.
토스트 예시 메세지