Complete the code to declare a class that inherits from another class.
class Dog: [1] { func bark() { print("Woof!") } }
In Swift, a class inherits from another class by specifying the parent class name after a colon.
Complete the code to override a method in a subclass.
class Animal { func sound() { print("Some sound") } } class Cat: Animal { override func [1]() { print("Meow") } }
The subclass overrides the method from the parent class by using the same method name.
Fix the error in the code by choosing the correct type that supports inheritance.
[1] Vehicle { var wheels: Int init(wheels: Int) { self.wheels = wheels } } class Car: Vehicle { var brand: String init(brand: String, wheels: Int) { self.brand = brand super.init(wheels: wheels) } }
Only classes can be inherited from in Swift. Structs cannot be used as a superclass.
Fill both blanks to create a subclass that calls the superclass initializer.
class Person { var name: String init(name: String) { self.name = name } } class Student: [1] { var id: Int init(name: String, id: Int) { self.id = id [2].init(name: name) } }
self.init instead of super.init to call the superclass initializer.The subclass Student inherits from Person and calls the superclass initializer using super.init.
Fill all three blanks to define a class-only protocol and a class that conforms to it.
protocol [1]: [2] { func play() } class Guitarist: [3] { func play() { print("Playing guitar") } }
Protocols can be restricted to classes by inheriting from AnyObject. The class then conforms to the protocol.