Overriding a method means providing a new version of a method in a subclass that replaces the version defined in its superclass.
Click to reveal answer
beginner
How do you indicate that a method or property is being overridden in Swift?
You use the override keyword before the method or property declaration in the subclass.
Click to reveal answer
intermediate
Can you override a property in Swift? If yes, what kinds of properties can be overridden?
Yes, you can override computed properties and property observers, but not stored properties directly.
Click to reveal answer
beginner
What happens if you try to override a method without using the override keyword?
Swift will give a compile-time error because it requires explicit use of override to avoid accidental overrides.
Click to reveal answer
intermediate
Explain how to call the superclass version of an overridden method in Swift.
Inside the overriding method, you call super.methodName() to run the original method from the superclass.
Click to reveal answer
Which keyword is required to override a method in Swift?
Aoverride
Bsuper
Cextends
Dfunc
✗ Incorrect
The override keyword tells Swift you are replacing a method or property from the superclass.
What will happen if you override a method but forget to write the override keyword?
ASwift gives a compile-time error
BThe program runs normally
CThe superclass method is called instead
DThe method is ignored
✗ Incorrect
Swift requires the override keyword to prevent accidental overrides, so it gives an error if missing.
How do you call the original method from the superclass inside an overridden method?
Aparent.methodName()
Bself.methodName()
Csuper.methodName()
Dbase.methodName()
✗ Incorrect
Using super.methodName() calls the superclass version of the method.
Which of these can be overridden in Swift?
AStored properties
BLocal variables
CConstants
DComputed properties
✗ Incorrect
You can override computed properties and property observers, but not stored properties directly.
Why is overriding useful in programming?
ATo reuse code without changes
BTo change or extend behavior of inherited methods
CTo create new classes
DTo delete superclass methods
✗ Incorrect
Overriding lets subclasses change or add to the behavior of methods they inherit.
Describe how to override a method in Swift and how to call the original method from the superclass.
Think about the keywords and syntax needed to replace and extend a method.
You got /3 concepts.
Explain what kinds of properties can be overridden in Swift and why stored properties are treated differently.
Consider how properties store or calculate values.
You got /3 concepts.
Practice
(1/5)
1.
What keyword must you use in Swift to change a method from a parent class in a subclass?
easy
A. override
B. super
C. extend
D. inherit
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 is override.
Final Answer:
override -> Option A
Quick Check:
Keyword to change parent method = override [OK]
Hint: Use 'override' before methods to change them in subclasses [OK]
Common Mistakes:
Using 'super' instead of 'override'
Forgetting the 'override' keyword
Using 'extend' which is not a Swift keyword
2.
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")
}
}
easy
A. override
B. super
C. func override
D. override func
Solution
Step 1: Recall Swift override syntax
In Swift, the keyword override comes before the func keyword.
Step 2: Match the correct syntax
The correct syntax is override func greet().
Final Answer:
override func -> Option D
Quick Check:
Keyword order is 'override func' [OK]
Hint: Write 'override' before 'func' to override methods [OK]
Common Mistakes:
Writing 'func override' instead of 'override func'
Using 'super' keyword instead of 'override'
Omitting 'override' keyword entirely
3.
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()
medium
A. Some sound
B. Bark
C. Compile error
D. Runtime error
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 overridden sound() method prints "Bark".
Final Answer:
Bark -> Option B
Quick Check:
Overridden method runs on subclass instance [OK]
Hint: Overridden methods run on subclass instances even if typed as parent [OK]
Common Mistakes:
Thinking parent method runs because variable type is Animal
Expecting compile or runtime error
Confusing method overriding with overloading
4.
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
}
medium
A. Missing 'override' keyword before 'var speed' in subclass
B. Cannot override properties in Swift
C. Property types must differ when overriding
D. Superclass property must be 'let' to override
Solution
Step 1: Check property overriding rules
To override a property in Swift, you must use the override keyword.
Step 2: Identify missing keyword
The subclass property speed lacks the override keyword, causing an error.
Final Answer:
Missing 'override' keyword before 'var speed' in subclass -> Option A
Quick Check:
Override properties need 'override' keyword [OK]
Hint: Always add 'override' before properties you change in subclass [OK]
Common Mistakes:
Forgetting 'override' keyword on properties
Thinking properties cannot be overridden
Changing property type when overriding
5.
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)
hard
A. Person
B. Student
C. Person - Student
D. Compile error due to super usage
Solution
Step 1: Understand property overriding with super
The subclass overrides description and calls super.description to get parent value.
Step 2: Combine parent and subclass strings
The returned string is "Person" + " - Student" = "Person - Student".
Final Answer:
Person - Student -> Option C
Quick Check:
super.property adds parent value [OK]
Hint: Use 'super' to include parent property value when overriding [OK]
Common Mistakes:
Expecting only 'Student' without parent text
Thinking 'super' causes compile error
Not using 'override' keyword on property
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.