0Pricing
Swift Academy · Lesson

override, final and Preventing Subclassing

Overriding methods/properties and sealing classes with final.

override, final and Preventing Subclassing is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

The `override` keyword lets subclasses replace superclass implementations. `final` prevents further overriding or subclassing, protecting your API contract.

override Methods

```swift class Animal { func speak() { print("...") } } class Cat: Animal { override func speak() { print("Meow") } } Cat().speak() // "Meow" ``` `override` is mandatory — the compiler rejects accidental shadowing.

override Properties

```swift class Shape { var area: Double { 0 } } class Square: Shape { var side: Double init(_ s: Double) { side = s } override var area: Double { side * side } } print(Square(4).area) // 16 ```

Calling super in Override

```swift class Logger { func log(_ msg: String) { print("[LOG] \(msg)") } } class TimedLogger: Logger { override func log(_ msg: String) { print(Date(), terminator: " ") super.log(msg) } } ```

final Methods and Properties

Mark a method or property `final` to prevent overriding: ```swift class BankAccount { final func transfer(amount: Double) { ... } // cannot override var balance: Double = 0 } ```

final Classes

Mark an entire class `final` to prevent subclassing: ```swift final class Singleton { static let shared = Singleton() private init() {} } // class Sub: Singleton { } ❌ ``` `final` also enables optimisations — the compiler can dispatch calls statically.

Why Use final?

Benefits of `final`: 1. Performance: static dispatch instead of virtual table lookup 2. Safety: API contract enforced — subclasses can't break your invariants 3. Documentation: signals the class is not designed for inheritance

Overriding Computed Properties

```swift class Vehicle { var description: String { "Vehicle" } } class Tesla: Vehicle { override var description: String { "Tesla - " + super.description } } print(Tesla().description) // "Tesla - Vehicle" ```

Overriding Property Observers

```swift class A { var x = 0 } class B: A { override var x: Int { didSet { print("B.x changed to \(x)") } } } var b = B() b.x = 5 // "B.x changed to 5" ```

Accidental Override Prevention

Without `override`, accidental shadowing is a compile error: ```swift class Parent { func greet() { } } class Child: Parent { func greet() { } // ❌ must write 'override' } ``` This protects against silent bugs when parent APIs evolve.

Quick Check

What keyword prevents a class from being subclassed?

Recap

Key takeaways: • `override` is required when replacing a superclass method/property • `super.method()` calls the parent's version • `final func/var` — prevents overriding that specific member • `final class` — prevents all subclassing • `final` enables static dispatch — a free performance win Next: type casting with is, as?, as!

Frequently asked questions

Is the “override, final and Preventing Subclassing” lesson free?

Yes — the full text of “override, final and Preventing Subclassing” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “override, final and Preventing Subclassing”?

Overriding methods/properties and sealing classes 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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “override, final and Preventing Subclassing” 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. Class Inheritance and the super Keyword
  2. override, final and Preventing Subclassing
  3. Type Casting: is, as?, as! and as
  4. Deinitializers and Memory Lifecycle
← Back to Swift Academy