Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'override' instead of 'final'.
Using 'open' which allows overriding.
Forgetting to add any keyword.
✗ Incorrect
Using 'final' before a method prevents subclasses from overriding it.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'open' which allows subclassing.
Using 'override' which is for methods, not classes.
Not adding any keyword.
✗ Incorrect
Marking a class as 'final' stops other classes from inheriting it.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'override' keyword on a final method.
Trying to change access level with 'public' or 'open'.
✗ Incorrect
You cannot override a final method; removing 'override' or marking method as 'final' in subclass is needed.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'open' which allows subclassing and overriding.
Mixing 'public' and 'override' keywords incorrectly.
✗ Incorrect
Marking both the class and method as 'final' stops subclassing and overriding.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The class and takeOff method are final to prevent subclassing and overriding; land method is public and can be overridden if subclassing was allowed.