0
0
Swiftprogramming~10 mins

Why inheritance is class-only in Swift - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why inheritance is class-only in Swift
Define class A
Define class B inheriting A
Create instance of B
Access properties/methods from A and B
Attempt inheritance with struct/enum
Compilation error: inheritance not allowed
Reason: structs/enums are value types, classes are reference types
Conclusion: inheritance only for classes
Inheritance works only with classes because classes are reference types that support shared behavior, while structs and enums are value types that do not support inheritance.
Execution Sample
Swift
class Animal {
    func sound() -> String { "Some sound" }
}

class Dog: Animal {
    override func sound() -> String { "Bark" }
}
Defines a base class Animal and a subclass Dog that inherits and overrides a method.
Execution Table
StepActionEvaluationResult
1Define class AnimalAnimal class createdAnimal class ready
2Define class Dog inheriting AnimalDog inherits AnimalDog class ready with Animal's methods
3Create instance dog of Dogdog is Dog instancedog can call sound()
4Call dog.sound()Dog's sound() overrides Animal's"Bark" returned
5Try inheritance with structstruct cannot inheritCompilation error
6Try inheritance with enumenum cannot inheritCompilation error
💡 Inheritance stops at structs/enums because they are value types and do not support inheritance.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
dognilDog instance createddog.sound() returns "Bark"dog remains Dog instance
Key Moments - 2 Insights
Why can't structs or enums inherit from other types?
Structs and enums are value types that copy data, so inheritance (which relies on shared references) is not supported, as shown in execution_table steps 5 and 6.
Why does Dog override Animal's sound() method?
Because Dog inherits from Animal (a class), it can override methods to provide specific behavior, as seen in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does dog.sound() return at step 4?
ACompilation error
B"Some sound"
C"Bark"
Dnil
💡 Hint
Check execution_table row with Step 4 where dog.sound() is called.
At which step does the code show inheritance is not allowed for structs?
AStep 3
BStep 5
CStep 2
DStep 6
💡 Hint
Look at execution_table rows for struct inheritance attempts.
If Dog was a struct instead of a class, what would happen?
AIt would cause a compilation error
BIt would inherit Animal successfully
CIt would override methods like a class
DIt would behave like a class instance
💡 Hint
Refer to execution_table steps 5 and 6 about inheritance restrictions.
Concept Snapshot
Inheritance in Swift works only with classes.
Classes are reference types allowing shared behavior.
Structs and enums are value types and cannot inherit.
Trying inheritance with structs/enums causes errors.
Use classes when you need inheritance.
Full Transcript
In Swift, inheritance is allowed only for classes because classes are reference types that share behavior through a common identity. Structs and enums are value types that copy data and do not support inheritance. The code example shows a base class Animal and a subclass Dog that overrides a method. When trying to inherit with structs or enums, the compiler gives an error. This is because inheritance relies on shared references, which value types do not have. Therefore, inheritance is class-only in Swift.