0
0
Swiftprogramming~10 mins

Overriding methods and properties in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Overriding methods and properties
Define base class
Define method/property
Define subclass
Override method/property
Create subclass instance
Call overridden method/property
Execute subclass version
End
This flow shows how a subclass replaces a base class method or property with its own version when called.
Execution Sample
Swift
class Animal {
    func sound() -> String {
        return "Some sound"
    }
}

class Dog: Animal {
    override func sound() -> String {
        return "Bark"
    }
}

let pet = Dog()
print(pet.sound())
This code defines a base class Animal with a method sound, then a subclass Dog overrides sound to return "Bark". It creates a Dog instance and prints its sound.
Execution Table
StepActionEvaluationResult
1Define class Animal with method sound()Method sound returns "Some sound"Animal.sound() ready
2Define subclass Dog overriding sound()Dog.sound() returns "Bark"Dog.sound() ready
3Create instance pet of Dogpet is Dog instancepet created
4Call pet.sound()Calls overridden Dog.sound()Returns "Bark"
5Print resultOutput printedBark
6EndNo more codeExecution stops
💡 Execution stops after printing overridden method result
Variable Tracker
VariableStartAfter Step 3Final
petnilDog instanceDog instance
Key Moments - 2 Insights
Why does pet.sound() print "Bark" instead of "Some sound"?
Because pet is an instance of Dog, and Dog overrides the sound() method. See execution_table step 4 where the overridden Dog.sound() is called.
What does the override keyword do?
It tells Swift that the subclass method replaces the base class method. Without override, the compiler gives an error. See execution_table step 2 where Dog.sound() is defined with override.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output printed at step 5?
A"Some sound"
B"Bark"
C"Woof"
DNo output
💡 Hint
Check the Result column at step 5 in execution_table.
At which step is the Dog instance created?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the Action column for instance creation in execution_table.
If we remove the override keyword from Dog.sound(), what happens?
ACompiler error occurs
BCode runs normally, prints "Bark"
CBase class method is called instead
DDog.sound() is ignored
💡 Hint
Recall key_moments explanation about the override keyword necessity.
Concept Snapshot
class BaseClass {
    func method() -> ReturnType { ... }
}

class SubClass: BaseClass {
    override func method() -> ReturnType { ... }
}

- Use override to replace base class methods or properties.
- Calls on subclass instances use overridden versions.
- Compiler enforces override keyword for safety.
Full Transcript
This example shows how a subclass in Swift can replace a method from its base class by overriding it. The base class Animal has a method sound() that returns "Some sound". The subclass Dog overrides sound() to return "Bark". When we create an instance of Dog and call sound(), the overridden Dog version runs, printing "Bark". The override keyword is required to tell Swift this is intentional. Without it, the code will not compile. This behavior lets subclasses customize or extend base class behavior safely.