← Back to Swift Course | Chapter 9: Inheritance & Protocols | Lesson 3 of 7

The super Keyword

super is how a subclass reaches back up to its parent class to use the parent's own version of something.
Syntax
swift
class Subclass: Superclass {
    var newProperty: Type

    init(newProperty: Type, inherited: Type) {
        self.newProperty = newProperty
        super.init(inherited: inherited)
    }

    override func method() { super.method() }
}

Calling super.init in a Subclass Initializer

When a subclass adds its own properties, its initializer typically sets those first, then calls super.init(...) to let the superclass initialize its own properties.

Example: Calling super.init in a Subclass Initializer

markup
// Define a class `Vehicle`
class Vehicle {
    var wheels: Int
    init(wheels: Int) {
        self.wheels = wheels
    }
}
// Define a class `Car`
class Car: Vehicle {
    var brand: String
    init(brand: String) {
        self.brand = brand
        super.init(wheels: 4)
    }
}
// Declare a constant `car`, set to `Car(brand: "Ford")`
let car = Car(brand: "Ford")
// Print "\(car.brand) has \(car.wheels) wheels" to the console
print("\(car.brand) has \(car.wheels) wheels")

Using super in an Overridden Method

super.methodName() inside an override calls the superclass's original implementation of that method.

Example: Using super in an Overridden Method

markup
// Define a class `Greeter`
class Greeter {
    // Define the function `greet` with no parameters and returning a String
    func greet() -> String {
        // Return `"Hello"` from this function
        return "Hello"
    }
}
// Define a class `EnthusiasticGreeter`
class EnthusiasticGreeter: Greeter {
    override func greet() -> String {
        // Return `super.greet() + "!!!"` from this function
        return super.greet() + "!!!"
    }
}
// Print `EnthusiasticGreeter().greet()` to the console
print(EnthusiasticGreeter().greet())
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting to call super.init(...) in a subclass's custom initializer when the superclass has its own required setup.
  2. Using super outside of a class (e.g. in a struct), where it has no meaning since structs don't support inheritance.
  3. Calling super.method() at the wrong point in an override, when the parent behavior was meant to run before or after new logic, not instead of it.
Chapter Summary
  • super refers to the current class's immediate superclass.
  • super.init(...) calls the superclass's initializer, often required to set up inherited properties.
  • super.method() invokes the superclass's version of an overridden method.
  • super can only be used within a subclass, never in a class with no superclass.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.