Overriding lets you change how a method or property works in a child class. It helps you customize behavior without changing the original code.
Overriding methods and properties in Swift
class Parent { func greet() { print("Hello from Parent") } } class Child: Parent { override func greet() { print("Hello from Child") } }
override keyword before the method or property you want to change.final in the parent class.Dog class changes the sound() method to print "Bark" instead of "Some sound".class Animal { func sound() { print("Some sound") } } class Dog: Animal { override func sound() { print("Bark") } }
Car class overrides the speed property to return a higher value.class Vehicle { var speed: Int { return 50 } } class Car: Vehicle { override var speed: Int { return 100 } }
Student class calls the original method with super and adds more text.class Person { func description() { print("I am a person") } } class Student: Person { override func description() { super.description() print("I am also a student") } }
This program shows how the Cat class changes the sound() method from the Animal class.
class Animal { func sound() { print("Animal makes a sound") } } class Cat: Animal { override func sound() { print("Meow") } } let myAnimal = Animal() myAnimal.sound() let myCat = Cat() myCat.sound()
Always use override to avoid mistakes and make your code clear.
You can call the original method or property using super if you want to keep some original behavior.
Overriding lets a subclass change methods or properties from its parent.
Use the override keyword before the method or property.
You can call the original version with super if needed.
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
