Inheritance / override / final / super
| Since: | Swift 1.0(2014) |
|---|
A Swift class can inherit from another class. Use the override keyword to override a method or property defined in a parent class.
Syntax
// Base class
class ParentClass {
func method() { }
var property: Type { get }
}
// Inheritance
class ChildClass: ParentClass {
// Override
override func method() {
super.method() // Call the parent implementation
// Additional logic
}
// Method that cannot be overridden further
final func finalMethod() { }
}
Syntax Overview
| Syntax | Description |
|---|---|
| class Child: Parent { } | Defines a child class that inherits from the parent class. |
| override func / var | Overrides a method or property defined in the parent class. |
| super.method() | Calls the parent class's implementation. |
| final class | Defines a class that cannot be subclassed. |
| final func / var | Defines a method or property that cannot be overridden. |
Sample Code
sample_inheritance_override.swift
// Base class
class Inspector {
var name: String
init(name: String) {
self.name = name
}
func duty() -> String {
return "On duty"
}
func describe() -> String {
return "\(name): \(duty())"
}
}
// Inheritance and override
class DivisionOneInspector: Inspector {
override func duty() -> String {
return "Investigating at Division 1"
}
// Additional method
func analyze() {
print("\(name) analyzed the Crime Coefficient with the Dominator")
}
}
class ChiefInspector: Inspector {
var division: String
init(name: String, division: String) {
self.division = division
super.init(name: name) // Always call super.init
}
override func duty() -> String {
return "Commanding as chief inspector"
}
override func describe() -> String {
let base = super.describe()
return "\(base) (\(division))"
}
}
let kogami = DivisionOneInspector(name: "Kogami Shinya")
let ginoza = ChiefInspector(name: "Ginoza Nobuchika", division: "Division 1")
print(kogami.describe())
print(ginoza.describe())
kogami.analyze()
// Polymorphism
let inspectors: [Inspector] = [kogami, ginoza, DivisionOneInspector(name: "Tsunemori Akane")]
for inspector in inspectors {
print(inspector.describe())
}
Running the above produces the following output:
swift inheritance_override.swift Kogami Shinya: Investigating at Division 1 Ginoza Nobuchika: Commanding as chief inspector (Division 1) Kogami Shinya analyzed the Crime Coefficient with the Dominator Kogami Shinya: Investigating at Division 1 Ginoza Nobuchika: Commanding as chief inspector (Division 1) Tsunemori Akane: Investigating at Division 1
Notes
Swift supports only single inheritance — a class cannot have more than one parent class. When a subclass overrides an initializer, it must call super.init().
Marking a class, method, or property with final prevents it from being overridden or subclassed, which can also improve performance. Defining a method with the same name as a parent class method without the override keyword causes a compile error. Always use the override keyword when intentionally overriding.
For details on property types, see Stored Properties / Computed Properties / lazy.
If you find any errors or copyright issues, please contact us.