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 Employee {
var name: String
init(name: String) {
self.name = name
}
func role() -> String {
return "On assignment"
}
func describe() -> String {
return "\(name): \(role())"
}
}
// Inheritance and override
class FullTimeEmployee: Employee {
override func role() -> String {
return "Working on projects in the dev department"
}
// Additional method
func report() {
print("\(name) created a progress report")
}
}
class Manager: Employee {
var department: String
init(name: String, department: String) {
self.department = department
super.init(name: name) // Always call super.init
}
override func role() -> String {
return "Leading the team as manager"
}
override func describe() -> String {
let base = super.describe()
return "\(base) (\(department))"
}
}
let member_x = FullTimeEmployee(name: "member_x")
let staff_a = Manager(name: "staff_a", department: "Dev Department")
print(member_x.describe())
print(staff_a.describe())
member_x.report()
// Polymorphism
let employees: [Employee] = [member_x, staff_a, FullTimeEmployee(name: "member_y")]
for employee in employees {
print(employee.describe())
}
Running the above produces the following output:
swift inheritance_override.swift member_x: Working on projects in the dev department staff_a: Leading the team as manager (Dev Department) member_x created a progress report member_x: Working on projects in the dev department staff_a: Leading the team as manager (Dev Department) member_y: Working on projects in the dev department
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.