0
0
Swiftprogramming~10 mins

Preventing overrides with final in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to prevent the method from being overridden.

Swift
class Vehicle {
    [1] func startEngine() {
        print("Engine started")
    }
}
Drag options to blanks, or click blank then click option'
Aopen
Boverride
Cfinal
Dpublic
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'override' instead of 'final'.
Using 'open' which allows overriding.
Forgetting to add any keyword.
2fill in blank
medium

Complete the code to prevent the class from being subclassed.

Swift
[1] class Animal {
    func sound() {
        print("Some sound")
    }
}
Drag options to blanks, or click blank then click option'
Afinal
Bopen
Cpublic
Doverride
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'open' which allows subclassing.
Using 'override' which is for methods, not classes.
Not adding any keyword.
3fill in blank
hard

Fix the error in the subclass trying to override a final method.

Swift
class Car {
    final func drive() {
        print("Driving")
    }
}

class SportsCar: Car {
    [1] func drive() {
        print("Fast driving")
    }
}
Drag options to blanks, or click blank then click option'
Aopen
Boverride
Cpublic
Dfinal
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'override' keyword on a final method.
Trying to change access level with 'public' or 'open'.
4fill in blank
hard

Fill both blanks to prevent subclassing and method overriding.

Swift
[1] class Bird {
    [2] func fly() {
        print("Flying")
    }
}
Drag options to blanks, or click blank then click option'
Afinal
Bopen
Cpublic
Doverride
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'open' which allows subclassing and overriding.
Mixing 'public' and 'override' keywords incorrectly.
5fill in blank
hard

Fill all three blanks to create a final class with a final method and a non-final method.

Swift
[1] class Plane {
    [2] func takeOff() {
        print("Taking off")
    }
    [3] func land() {
        print("Landing")
    }
}
Drag options to blanks, or click blank then click option'
Afinal
Bopen
Coverride
Dpublic
Attempts:
3 left
💡 Hint
Common Mistakes
Marking the class as 'open' which allows subclassing.
Using 'override' keyword incorrectly on methods.
Not marking the method as 'final' to prevent overriding.