← 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.

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
class Vehicle {
    var wheels: Int
    init(wheels: Int) {
        self.wheels = wheels
    }
}
class Car: Vehicle {
    var brand: String
    init(brand: String) {
        self.brand = brand
        super.init(wheels: 4)
    }
}
let car = Car(brand: "Ford")
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
class Greeter {
    func greet() -> String {
        return "Hello"
    }
}
class EnthusiasticGreeter: Greeter {
    override func greet() -> String {
        return super.greet() + "!!!"
    }
}
print(EnthusiasticGreeter().greet())
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.