100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Swift

Inheritance in Swift

Inheritance lets a class build on a superclass's properties and methods, overriding behavior with the override keyword.

Structures & ClassesIntermediate9 min readJul 8, 2026
Analogies

Introduction

Inheritance is a class-only feature in Swift that lets one class, the subclass, acquire the properties, methods, and other characteristics of another class, the superclass. This enables code reuse and the modeling of 'is-a' relationships, such as a Dog being a kind of Animal. Swift supports only single inheritance — a class can have at most one direct superclass — unlike some languages that permit multiple inheritance.

🏏

Cricket analogy: An all-rounder like Ravindra Jadeja inherits both a bowler's and batsman's traits from a shared cricketer 'superclass,' modeling an 'is-a' relationship, but Swift only allows one direct superclass, unlike mixing traits from two unrelated sports.

Syntax

swift
class Animal {
    var name: String
    init(name: String) { self.name = name }

    func makeSound() {
        print("Some generic animal sound")
    }
}

class Dog: Animal {
    override func makeSound() {
        print("\(name) says Woof!")
    }
}

Explanation

Dog: Animal declares Dog as a subclass of Animal, so Dog automatically inherits the name property and the makeSound() method. The override keyword is mandatory whenever a subclass provides its own implementation of a method, property, or subscript that already exists in the superclass; omitting override when you mean to override, or using it when there is nothing to override, is a compile-time error. Inside an overriding method, super.makeSound() can be called to invoke the superclass's original implementation before or after adding new behavior. The final keyword can be applied to a class, method, or property to prevent it from being subclassed or overridden any further.

🏏

Cricket analogy: 'FastBowler: Bowler' inherits the bowl() method automatically, but adding a signature yorker requires the mandatory 'override' keyword, and calling 'super.bowl()' first, like Jasprit Bumrah adding his variation atop the base action; 'final' would stop anyone subclassing FastBowler further.

Example

swift
class Animal {
    var name: String
    init(name: String) { self.name = name }
    func makeSound() { print("...") }
}

class Dog: Animal {
    override func makeSound() {
        super.makeSound()
        print("\(name) says Woof!")
    }
}

let d = Dog(name: "Rex")
d.makeSound()

Output

swift
...
Rex says Woof!

Key Takeaways

  • Only classes support inheritance in Swift; structs and enums do not.
  • A class can have at most one direct superclass — single inheritance only.
  • The override keyword is required when redefining an inherited method or property.
  • super.method() invokes the superclass's implementation from within an override.
  • final prevents a class from being subclassed, or a method/property from being overridden further.
  • Subclasses inherit stored properties, computed properties, and methods from their superclass.

Practice what you learned

Was this page helpful?

Topics covered

#Swift#SwiftProgrammingStudyNotes#Programming#InheritanceInSwift#Inheritance#Syntax#Explanation#Example#OOP#StudyNotes#SkillVeris