Swift Academy · Lesson

Class Inheritance and the super Keyword

Subclassing, calling super.init() and extending superclass behaviour.

Lesson 1 of 412 steps

Class Inheritance and the super Keyword is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 4. 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 4 lessons in total.

Welcome

Classes support inheritance — a subclass can extend or specialise a superclass. This enables code reuse and polymorphism, core pillars of object-oriented design.

Defining a Subclass

```swift class Animal { var name: String init(name: String) { self.name = name } func speak() { print("...") } } class Dog: Animal { func fetch() { print("\(name) fetches!") } } let d = Dog(name: "Rex") d.speak() // "..." d.fetch() // "Rex fetches!" ```

super.init() — Required Call

```swift class Vehicle { var speed: Double init(speed: Double) { self.speed = speed } } class Car: Vehicle { var brand: String init(brand: String, speed: Double) { self.brand = brand super.init(speed: speed) // must call super } } ```

Two-Phase Initialisation

Swift's two-phase init rules: 1. All *own* stored properties must be set *before* calling `super.init()` 2. After `super.init()`, you can customise inherited properties This prevents you from using uninitialised memory.

Calling Super Methods

```swift class Shape { func draw() { print("Drawing shape") } } class Circle: Shape { override func draw() { super.draw() // call parent implementation print("Drawing circle") } } ```

Inheriting Properties

Subclasses inherit all superclass properties: ```swift class Person { var name: String = "" var age: Int = 0 } class Employee: Person { var company: String = "" } let e = Employee() e.name = "Alice" e.company = "Acme" ```

Designated vs Convenience Initialisers

• *Designated* — fully initialises all properties; calls `super.init()` • *Convenience* — delegates to another init with `self.init()` ```swift convenience init(name: String) { self.init(name: name, age: 0) // delegates to designated } ```

required init

Mark an init `required` to force all subclasses to implement it: ```swift class Base { required init(id: Int) { ... } } class Sub: Base { required init(id: Int) { super.init(id: id) } } ``` Common when a factory method needs to create instances via protocol.

Class Hierarchy and Type

```swift let a: Animal = Dog(name:"Buddy") print(a is Dog) // true print(a is Animal) // true ``` A `Dog` IS-A `Animal` — subclass instances satisfy superclass type requirements.

Single Inheritance

Swift classes support only *single* inheritance (one superclass). But a class can conform to many protocols: ```swift class MyVC: UIViewController, UITableViewDelegate, UITableViewDataSource { } ``` Protocols are Swift's mechanism for multiple inheritance of behaviour.

Quick Check

In a subclass designated init, when must you call `super.init()`?

Recap

Key takeaways: • Subclass with `class Sub: Super` • Call `super.init()` after setting own properties • Two-phase init: own properties first, then super • `super.method()` calls the parent implementation • Swift has single inheritance; use protocols for multiple behaviour Next: override, final, and preventing subclassing.
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 “Class Inheritance and the super Keyword” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Class Inheritance and the super Keyword”, 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 4 lessons in total.

What will I learn in “Class Inheritance and the super Keyword”?

Subclassing, calling super.init() and extending superclass behaviour. 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 1 of 4.

How long does the “Class Inheritance and the super Keyword” 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