What if you could change just one part of your code and instantly create a whole new behavior without rewriting everything?
Why Overriding methods and properties in Swift? - Purpose & Use Cases
Imagine you have a basic car model in your code, and you want to create a sports car that behaves a little differently. Without overriding, you'd have to rewrite the entire car code for the sports car, even if most parts are the same.
Manually copying and changing code for each new car type is slow and confusing. It's easy to make mistakes, and if you fix a bug in the basic car, you have to fix it again in every copy. This wastes time and causes errors.
Overriding lets you keep the basic car code and only change the parts that need to be different in the sports car. This means less code, fewer mistakes, and easier updates.
class Car { func drive() { print("Driving normally") } } class SportsCar { func drive() { print("Driving fast") } }
class Car { func drive() { print("Driving normally") } } class SportsCar: Car { override func drive() { print("Driving fast") } }
It enables you to build flexible, reusable code that adapts easily to new needs without rewriting everything.
Think of a video game where different characters share common moves but have special attacks. Overriding lets each character keep the common moves and customize their special attacks easily.
Overriding saves time by reusing code.
It reduces errors by changing only what's needed.
It makes your code easier to update and extend.
Practice
What keyword must you use in Swift to change a method from a parent class in a subclass?
Solution
Step 1: Understand method overriding in Swift
Swift requires a special keyword to indicate a method is being changed in a subclass.Step 2: Identify the correct keyword
The keyword to override a method or property isoverride.Final Answer:
override -> Option AQuick Check:
Keyword to change parent method = override [OK]
- Using 'super' instead of 'override'
- Forgetting the 'override' keyword
- Using 'extend' which is not a Swift keyword
Which of the following is the correct syntax to override a method named greet() in Swift?
class Parent {
func greet() {
print("Hello")
}
}
class Child: Parent {
??? func greet() {
print("Hi")
}
}Solution
Step 1: Recall Swift override syntax
In Swift, the keywordoverridecomes before thefunckeyword.Step 2: Match the correct syntax
The correct syntax isoverride func greet().Final Answer:
override func -> Option DQuick Check:
Keyword order is 'override func' [OK]
- Writing 'func override' instead of 'override func'
- Using 'super' keyword instead of 'override'
- Omitting 'override' keyword entirely
What will be the output of the following Swift code?
class Animal {
func sound() {
print("Some sound")
}
}
class Dog: Animal {
override func sound() {
print("Bark")
}
}
let pet: Animal = Dog()
pet.sound()Solution
Step 1: Understand method overriding and polymorphism
Even though the variable type is Animal, the actual object is Dog, so overridden method runs.Step 2: Identify which method is called
The Dog's overriddensound()method prints "Bark".Final Answer:
Bark -> Option BQuick Check:
Overridden method runs on subclass instance [OK]
- Thinking parent method runs because variable type is Animal
- Expecting compile or runtime error
- Confusing method overriding with overloading
Identify the error in this Swift code that tries to override a property:
class Vehicle {
var speed: Int = 0
}
class Car: Vehicle {
var speed: Int = 100
}Solution
Step 1: Check property overriding rules
To override a property in Swift, you must use theoverridekeyword.Step 2: Identify missing keyword
The subclass propertyspeedlacks theoverridekeyword, causing an error.Final Answer:
Missing 'override' keyword before 'var speed' in subclass -> Option AQuick Check:
Override properties need 'override' keyword [OK]
- Forgetting 'override' keyword on properties
- Thinking properties cannot be overridden
- Changing property type when overriding
Given this Swift code, what will be printed?
class Person {
var description: String {
return "Person"
}
}
class Student: Person {
override var description: String {
return super.description + " - Student"
}
}
let s = Student()
print(s.description)Solution
Step 1: Understand property overriding with super
The subclass overridesdescriptionand callssuper.descriptionto get parent value.Step 2: Combine parent and subclass strings
The returned string is "Person" + " - Student" = "Person - Student".Final Answer:
Person - Student -> Option CQuick Check:
super.property adds parent value [OK]
- Expecting only 'Student' without parent text
- Thinking 'super' causes compile error
- Not using 'override' keyword on property
Ready to prove it?
Pass the test and this pattern moves unknown
