Using final stops other parts of your code from changing a class or method. This helps keep your code safe and clear.
Preventing overrides with final in Swift
final class ClassName { // class code } class ParentClass { final func methodName() { // method code } }
You put final before class to stop the class from being inherited.
You put final before a method to stop it from being overridden in child classes.
Animal cannot be inherited by any other class.final class Animal { func sound() { print("Some sound") } }
start() cannot be changed in Car because it is marked final in Vehicle.class Vehicle { final func start() { print("Starting engine") } } class Car: Vehicle { // Trying to override start() here will cause an error }
This program shows a Bird class with a final method fly(). The Eagle class tries to override it but cannot. When we call fly() on an Eagle object, it runs the original method.
class Bird { final func fly() { print("Flying high") } } class Eagle: Bird { // Uncommenting the below code will cause an error // override func fly() { // print("Eagle flying") // } } let eagle = Eagle() eagle.fly()
Marking a class final means no other class can inherit from it.
Marking a method final means child classes cannot change how it works.
Using final can help your app run faster because Swift knows the code won't change.
final stops classes or methods from being changed.
Use it to protect important code and keep your design clear.
It can also help your program run better.
Practice
What does the final keyword do in Swift?
Solution
Step 1: Understand the purpose of
Thefinalfinalkeyword in Swift is used to stop classes or methods from being changed by subclasses.Step 2: Identify what
It prevents overriding methods or subclassing classes, ensuring the code stays as originally designed.finalpreventsFinal Answer:
Prevents a class or method from being overridden or subclassed -> Option DQuick Check:
finalstops overrides = A [OK]
- Confusing final with constant variables
- Thinking final allows subclassing
- Mixing final with async keyword
Which of the following is the correct way to declare a method that cannot be overridden in Swift?
class Vehicle {
_____ func startEngine() {
print("Engine started")
}
}Solution
Step 1: Identify the keyword to prevent overriding
In Swift, thefinalkeyword is used before a method to stop it from being overridden in subclasses.Step 2: Check the method declaration
Placingfinalbeforefunccorrectly marks the method as non-overridable.Final Answer:
final -> Option CQuick Check:
Usefinalbefore func to prevent override [OK]
- Using override keyword incorrectly
- Confusing static with final
- Using mutating which is for structs
What will happen when you run this Swift code?
class Animal {
final func sound() {
print("Animal sound")
}
}
class Dog: Animal {
override func sound() {
print("Bark")
}
}
let pet = Dog()
pet.sound()Solution
Step 1: Identify the use of final in the base class
The methodsound()inAnimalis marked asfinal, so it cannot be overridden.Step 2: Check the subclass method override
TheDogclass tries to overridesound(), which causes a compile-time error because overriding a final method is not allowed.Final Answer:
Compile-time error due to overriding a final method -> Option BQuick Check:
Overriding final method = Compile error [OK]
- Assuming it prints "Bark"
- Thinking it prints base method output
- Expecting runtime error instead of compile error
Find the error in this Swift code and fix it:
final class Vehicle {
func drive() {
print("Driving")
}
}
class Car: Vehicle {
override func drive() {
print("Car driving")
}
}Solution
Step 1: Identify the use of final on the Vehicle class
TheVehicleclass is marked asfinal, so it cannot be subclassed.Step 2: Check subclass declaration
TheCarclass tries to subclassVehicle, which causes a compile-time error because subclassing a final class is not allowed.Final Answer:
Removefinalfrom Vehicle class -> Option AQuick Check:
Subclassing final class = Error; remove final [OK]
- Trying to override without removing final
- Adding final to subclass instead of base
- Changing class to struct unnecessarily
You want to create a base class BankAccount with a method calculateInterest() that should never be changed by subclasses. However, subclasses can add new methods. How should you declare BankAccount and calculateInterest()?
Solution
Step 1: Understand the requirement for subclassing
The base classBankAccountshould allow subclasses, so it must NOT befinal.Step 2: Protect the method from being overridden
MarkcalculateInterest()asfinalto prevent subclasses from changing it.Final Answer:
DeclareBankAccountnormally andcalculateInterest()asfinal-> Option AQuick Check:
final method, normal class = C [OK]
- Making the whole class final, blocking subclassing
- Not marking method as final, allowing override
- Using struct instead of class incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
