Thuta Learning
Swift
AdvancedProgrammingBeginner

Inheritance

SwiftLesson 2025 min

What you'll walk away with

  • Explain the type, ownership, and runtime behavior of Inheritance
  • Test nil, error, boundary, and concurrency cases
  • Write safe and testable Swift

Explain it simply

Inheritance, in plain language

Inheritance is an OOP concept where one class inherits the properties and methods of another class. In Swift, only classes support inheritance — structs and enums don't have it.

Problem → Why → Solution

The problem — why this concept matters

You can't inherit from a struct. For most data models, consider struct + protocol first.

A common mistake

Assuming one sample output proves nil, ownership, actor isolation, and every error path.

Core concept — understand how it works

Use Inheritance to express stable capabilities and reusable algorithms. Prefer protocol-oriented design, constrained generics, and focused extensions to reduce coupling to concrete types, inheritance, and global state.

Mental model
  1. typed value
  2. Swift expression or function
  3. safe result

Explain the type, ownership, and runtime behavior of Inheritance

Core syntax or pattern

swift
class Notification {
    func send() {
        print("Sending notification...")
    }
}

class EmailNotification: Notification {
    override func send() {
        print("Sending email notification")
    }
}

let email = EmailNotification()
email.send()

Use Inheritance to express stable capabilities and reusable algorithms. Prefer protocol-oriented design, constrained generics, and focused extensions to reduce coupling to concrete types, inheritance, and global state.

Read the code step by step

  1. typed valueclass Notification {

    Inheritance is an OOP concept where one class inherits the properties and methods of another class. In Swift, only classes support inheritance — structs and enums don't have it.

  2. Swift expression or functionoverride func send() {

    Use Inheritance to express stable capabilities and reusable algorithms. Prefer protocol-oriented design, constrained generics, and focused extensions to reduce coupling to concrete types, inheritance, and global state.

  3. safe resultemail.send()

    Run and test an original Inheritance example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

Execution flow
  1. typed value
  2. Swift expression or function
  3. safe result

Success

Explain the type, ownership, and runtime behavior of Inheritance

Failure

You can't inherit from a struct. For most data models, consider struct + protocol first.

Inheritance is an OOP concept where one class inherits the properties and methods of another class. In Swift, only classes support inheritance — structs and enums don't have it.

Inheritance is great for reusing shared behavior, but overusing it when it's not really needed can make your code harder to understand. In Swift, protocol composition with structs is also widely used as an alternative.

swift
class Notification {
    func send() {
        print("Sending notification...")
    }
}

class EmailNotification: Notification {
    override func send() {
        print("Sending email notification")
    }
}

let email = EmailNotification()
email.send()

EmailNotification inherits from Notification. Since we want to give the parent class's send() method its own behavior in the child class, override is used.

You should see
Sending email notification

Real-world usage

How to apply it in a real project

Run and test an original Inheritance example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

Mini challenge

Build it yourself

Run and test an original Inheritance example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
swift
class Notification {
    func send() {
        print("Sending notification...")
    }
}

class EmailNotification: Notification {
    override func send() {
        print("Sending email notification")
    }
}

let email = EmailNotification()
email.send()

Expected: Explain the type, ownership, and runtime behavior of Inheritance

Show hint

Run and test an original Inheritance example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

Quick quiz 1 / 3

What best describes the core idea of Inheritance?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • typed value → Swift expression or function → safe result
  • Use Inheritance to express stable capabilities and reusable algorithms. Prefer protocol-oriented design, constrained generics, and focused extensions to reduce coupling to concrete types, inheritance, and global state.
  • You can't inherit from a struct. For most data models, consider struct + protocol first.
  • Run and test an original Inheritance example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

The Swift Programming Language — Swift.org

Easy traps

  • You can't inherit from a struct. For most data models, consider struct + protocol first.
  • Assuming one sample output proves nil, ownership, actor isolation, and every error path.
  • Bypassing compiler safety with force unwraps or unchecked shared mutable state.

Hands-on Exercise

Run and test an original Inheritance example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

You'll know it worked when: Sending email notification

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Inheritance | Thuta Learning