0Pricing
Swift Academy · Lesson

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

  1. Memberwise init & mutating methods
  2. Inheritance, overriding & final
  3. Access control (public/internal/fileprivate/private)
← Back to Swift Academy