0
0
Swiftprogramming~20 mins

Why inheritance is class-only in Swift - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why does Swift restrict inheritance to classes only?
In Swift, only classes can inherit from other classes. Why is inheritance limited to classes and not allowed for structs or enums?
ABecause structs and enums are slower than classes and cannot inherit.
BBecause classes support reference semantics which allow shared mutable state, making inheritance meaningful.
CBecause Swift does not allow any type to inherit from another type.
DBecause inheritance is only useful for value types like structs and enums.
Attempts:
2 left
💡 Hint
Think about how classes and structs differ in how they store and share data.
Predict Output
intermediate
2:00remaining
What is the output of this Swift code involving class inheritance?
Consider the following Swift code. What will be printed when it runs?
Swift
class Animal {
    func sound() -> String {
        return "Some sound"
    }
}

class Dog: Animal {
    override func sound() -> String {
        return "Bark"
    }
}

let pet: Animal = Dog()
print(pet.sound())
A"Bark"
B"Some sound"
CCompilation error due to missing override keyword
DRuntime error due to type mismatch
Attempts:
2 left
💡 Hint
Remember how method overriding works with class inheritance and polymorphism.
🔧 Debug
advanced
2:00remaining
Identify the error when trying to inherit from a struct in Swift
What error will this Swift code produce and why?
Swift
struct Vehicle {
    var wheels: Int
}

struct Car: Vehicle {
    var brand: String
}
AError: Inheritance from non-class type 'Vehicle' is not allowed
BError: Missing initializer for struct 'Car'
CError: Cannot override stored property 'wheels'
DNo error, code compiles and runs fine
Attempts:
2 left
💡 Hint
Recall which types support inheritance in Swift.
📝 Syntax
advanced
2:00remaining
Which Swift code snippet correctly overrides a method in a subclass?
Choose the code snippet that correctly overrides the describe() method in a subclass.
A
class Parent {
    func describe() { print("Parent") }
}
class Child: Parent {
    override describe() { print("Child") }
}
B
class Parent {
    func describe() { print("Parent") }
}
class Child: Parent {
    func describe() { print("Child") }
}
C
class Parent {
    func describe() { print("Parent") }
}
class Child: Parent {
    override func describe() { print("Child") }
}
D
class Parent {
    func describe() { print("Parent") }
}
class Child: Parent {
    override func describe() -> String { return "Child" }
}
Attempts:
2 left
💡 Hint
Remember the syntax for method overriding in Swift requires the 'override' keyword and matching method signature.
🚀 Application
expert
3:00remaining
How does Swift's class-only inheritance affect protocol design?
Given that inheritance is class-only in Swift, which approach best allows sharing default behavior across multiple types including structs and enums?
AUse multiple inheritance with classes to share behavior.
BUse class inheritance for all types including structs and enums.
CUse global functions instead of protocols or inheritance.
DUse protocols with default implementations via protocol extensions.
Attempts:
2 left
💡 Hint
Think about how Swift encourages code reuse without class inheritance.