Overriding methods and properties in Swift - Time & Space Complexity
When we override methods or properties in Swift, we want to know how this affects the time it takes for our program to run.
We ask: Does overriding change how long the program takes as it grows?
Analyze the time complexity of the following Swift code using method overriding.
class Animal {
func speak() {
print("Animal sound")
}
}
class Dog: Animal {
override func speak() {
print("Bark")
}
}
let pet: Animal = Dog()
pet.speak()
This code shows a base class with a method and a subclass that overrides it. We call the method on a subclass instance typed as the base class.
Look for repeated actions or calls that affect time.
- Primary operation: Calling the
speak()method once. - How many times: Exactly one time in this example.
Since the method is called once, the time does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays the same no matter how many objects or calls you have if you call the method once.
Time Complexity: O(1)
This means the time to run the overridden method call stays constant, no matter the input size.
[X] Wrong: "Overriding a method makes the program slower as input grows because it adds extra steps."
[OK] Correct: Overriding just changes which code runs, but calling the method still takes the same amount of time each call.
Understanding that overriding does not increase time complexity helps you explain how object-oriented features work efficiently in real programs.
"What if the overridden method calls a loop inside it? How would the time complexity change?"
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
