Inheritance, overriding & final
Create a base class, extend it via inheritance, override methods/properties, call super , and lock behavior with final .
Overview
Swift classes support inheritance. You can override behavior and call super. Use final to lock things down.
Base class
Create a base class with an instance method that subclasses can override.
class Animal {
var name: String
init(name: String) { self.name = name }
func speak() -> String { "..." } // to be overridden
}
let a = Animal(name: "Thing")
print(a.speak())All lessons in this course
- Memberwise init & mutating methods
- Inheritance, overriding & final
- Access control (public/internal/fileprivate/private)