Discover why Swift trusts only classes to carry the power of inheritance and how it keeps your code safe and clean!
Why inheritance is class-only in Swift - The Real Reasons
Imagine you want to create different types of animals in your app, like dogs and cats, and share some common features like making sounds or moving. Without inheritance, you'd have to copy and paste the same code for each animal type, which quickly becomes messy and hard to manage.
Copying code for each type means if you want to change a shared behavior, you must update it everywhere. This is slow and error-prone. Also, without a clear hierarchy, your code becomes confusing and hard to extend.
Inheritance lets you create a base class with shared features, and other classes can build on it. Swift limits inheritance to classes because classes support reference behavior and identity, which are essential for inheritance to work correctly and safely.
struct Dog { func sound() { print("Bark") } }
struct Cat { func sound() { print("Meow") } }class Animal { func sound() { print("Some sound") } } class Dog: Animal { override func sound() { print("Bark") } } class Cat: Animal { override func sound() { print("Meow") } }
Inheritance in classes allows you to create clear, reusable, and extendable code structures that model real-world relationships easily.
Think of a vehicle app where you have a base Vehicle class with common features like speed and fuel, and subclasses like Car and Bike that add their own unique behaviors.
Inheritance helps share and extend code efficiently.
Swift restricts inheritance to classes for safety and clarity.
This makes your code easier to maintain and grow.
Practice
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 CQuick Check:
Inheritance = Reference types only [OK]
- Thinking structs can inherit like classes
- Believing enums can have inheritance
- Confusing methods with inheritance
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 AQuick Check:
Inheritance syntax = class only [OK]
- Trying to inherit a class in a struct
- Using enum to inherit a class
- Confusing protocol inheritance with class inheritance
struct ParentStruct {}
struct ChildStruct: ParentStruct {}Solution
Step 1: Analyze struct inheritance rules
Structs in Swift cannot inherit from other structs or classes.Step 2: Predict compiler behavior
The compiler will produce an error because inheritance is not allowed for structs.Final Answer:
Compile-time error: structs cannot inherit -> Option BQuick Check:
Struct inheritance = error [OK]
- Assuming structs inherit like classes
- Expecting runtime error instead of compile error
- Thinking structs copy properties via inheritance
enum ParentEnum {}
class ChildClass: ParentEnum {}Solution
Step 1: Check inheritance rules for enums and classes
Enums are value types and cannot be inherited or used as base classes.Step 2: Understand class inheritance restrictions
Classes can only inherit from other classes, not enums.Step 3: Combine observations
All statements about enums and classes here are correct.Final Answer:
All of the above -> Option DQuick Check:
Enum inheritance = disallowed [OK]
- Thinking enums can be base classes
- Confusing enum cases with inheritance
- Ignoring that classes can't inherit enums
Solution
Step 1: Recall inheritance limits
Inheritance works only for classes, so structs cannot inherit from classes.Step 2: Identify alternative sharing method
Protocols allow both structs and classes to share behavior by conforming to them.Step 3: Evaluate options
Using protocols is the best way to share behavior without inheritance.Final Answer:
Use a protocol to define shared behavior -> Option AQuick Check:
Protocols share behavior across types [OK]
- Trying to inherit struct from class
- Converting structs unnecessarily to classes
- Thinking enums can inherit
Ready to prove it?
Pass the test and this pattern moves unknown
