What does it mean that inheritance is class-only in Swift?
In Swift, only classes can inherit from other classes. Structures and enums cannot use inheritance. This means you can create a new class based on an existing class, but you cannot do this with structs or enums.
Click to reveal answer
intermediate
Why can't structs and enums use inheritance in Swift?
Structs and enums are value types in Swift, which means they are copied when assigned or passed around. Inheritance requires reference semantics, where one object can share behavior and state with another. Because structs and enums are copied, inheritance doesn't fit their design.
Click to reveal answer
intermediate
How does Swift's use of protocols relate to inheritance?
Swift uses protocols to share behavior across structs, enums, and classes. Protocols allow different types to conform to the same set of rules without inheritance. This provides flexibility without needing inheritance for value types.
Click to reveal answer
beginner
What is a key difference between classes and structs in Swift regarding memory?
Classes are reference types, meaning multiple variables can point to the same instance in memory. Structs are value types, so each variable has its own copy. This difference is why inheritance works only with classes.
Click to reveal answer
beginner
Can you explain inheritance in Swift using a real-life example?
Think of a class as a blueprint for a car. You can create a new blueprint for a sports car that inherits features from the basic car blueprint but adds new features. Structs are like photocopies of a drawing; each copy is independent and can't inherit changes from others.
Click to reveal answer
Which Swift type supports inheritance?
AClass
BStruct
CEnum
DProtocol
✗ Incorrect
Only classes in Swift support inheritance. Structs and enums do not.
Why is inheritance limited to classes in Swift?
ABecause structs are faster
BBecause classes are reference types and support shared behavior
CBecause enums are only for numbers
DBecause protocols replace inheritance
✗ Incorrect
Inheritance requires reference semantics, which classes have. Structs and enums are value types and do not support inheritance.
What Swift feature allows sharing behavior without inheritance?
AGenerics
BExtensions
CProtocols
DClosures
✗ Incorrect
Protocols define rules that types can follow, allowing shared behavior without inheritance.
What happens when you assign a struct to a new variable in Swift?
AA copy is made
BA reference is shared
CThe original is deleted
DInheritance is triggered
✗ Incorrect
Structs are value types, so assigning them creates a copy.
Which statement is true about classes in Swift?
AThey are the same as structs
BThey are value types and do not support inheritance
CThey cannot have methods
DThey are reference types and support inheritance
✗ Incorrect
Classes are reference types and can inherit from other classes.
Explain why inheritance is limited to classes in Swift and not available for structs or enums.
Think about how data is copied or shared in Swift types.
You got /4 concepts.
Describe how Swift uses protocols to share behavior without inheritance.
Protocols are like contracts that types agree to follow.
You got /4 concepts.
Practice
(1/5)
1. Why is inheritance only allowed for classes in Swift?
easy
A. Because only classes can have properties
B. Because structs and enums are faster to create
C. Because classes are reference types and share data by reference
D. Because structs and enums cannot have methods
Solution
Step 1: Understand type behavior in Swift
Classes are reference types, meaning instances share the same data in memory.
Step 2: Compare with structs and enums
Structs and enums are value types, so they copy data instead of sharing it.
Final Answer:
Because classes are reference types and share data by reference -> Option C
Quick Check:
Inheritance = Reference types only [OK]
Hint: Inheritance needs shared data, so only classes can do it [OK]
Common Mistakes:
Thinking structs can inherit like classes
Believing enums can have inheritance
Confusing methods with inheritance
2. Which Swift declaration correctly shows inheritance allowed only for classes?
easy
A. class MyClass: ParentClass {}
B. struct MyStruct: ParentClass {}
C. enum MyEnum: ParentClass {}
D. protocol MyProtocol: ParentClass {}
Solution
Step 1: Identify inheritance syntax in Swift
Only classes can inherit from other classes using the colon syntax.
Step 2: Check each declaration
Structs, enums, and protocols cannot inherit from classes.
Final Answer:
class MyClass: ParentClass {} -> Option A
Quick Check:
Inheritance syntax = class only [OK]
Hint: Only classes use colon for inheritance from classes [OK]
Common Mistakes:
Trying to inherit a class in a struct
Using enum to inherit a class
Confusing protocol inheritance with class inheritance