0Pricing
Swift Academy · Lesson

Class Inheritance and the super Keyword

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

Class Inheritance and the super Keyword is a free Swift Academy lesson on CoddyKit — lesson 1 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

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.

Frequently asked questions

Is the “Class Inheritance and the super Keyword” lesson free?

Yes — the full text of “Class Inheritance and the super Keyword” 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 “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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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