Swift Academy · Lesson

Inheritance, overriding & final

Create a base class, extend it via inheritance, override methods/properties, call super , and lock behavior with final .

Lesson 2 of 38 steps

Inheritance, overriding & final is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 3 lessons in total.

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())

Override method

Use the override keyword when providing a new implementation in a subclass.

class Dog: Animal {
    override func speak() -> String { "woof" }
}
let d = Dog(name: "Rex")
print(d.speak())  // "woof"

super call

Call super to reuse base behavior and extend it in the subclass.

class Cat: Animal {
    override func speak() -> String {
        let base = super.speak()
        return base + " meow"
    }
}
print(Cat(name: "Mimi").speak())  // "... meow"

Override & final

Override properties and methods. Use final on a class or member to stop further subclassing/overriding.

class Vehicle {
    var description: String { "vehicle" }        // computed property
    func drive() { print("move") }
}
class Car: Vehicle {
    override var description: String { "car" }   // override property
    override func drive() { print("vroom") }
}
final class ElectricCar: Car {                   // cannot be subclassed
    final override func drive() { print("zzzz") } // cannot be overridden further
}
let e = ElectricCar()
print(e.description)
e.drive()

Notes & tips

Notes:

  • Prefer composition over deep hierarchies.
  • Mark classes or members final unless you intend extension by subclassing.
  • Always use override when replacing base behavior.

final keyword

Quick check: Which keyword blocks subclassing/overriding?

Recap

Recap: Define a base class, override methods/properties in subclasses, call super to reuse work, and mark types/members final to lock behavior.

Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Inheritance, overriding & final” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Inheritance, overriding & final”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 3 lessons in total.

What will I learn in “Inheritance, overriding & final”?

Create a base class, extend it via inheritance, override methods/properties, call super , and lock behavior with final . You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.

How long does the “Inheritance, overriding & final” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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