📖 Scenario: Imagine you are building a simple app to manage different types of vehicles. You want to share common features like speed and capacity but also have specific features for cars and bicycles.
🎯 Goal: Learn why inheritance in Swift works only with classes and not with structs or enums by creating a simple class hierarchy.
📋 What You'll Learn
Create a base class called Vehicle with properties speed and capacity
Create a subclass called Car that inherits from Vehicle and adds a property brand
Create a subclass called Bicycle that inherits from Vehicle and adds a property hasBasket
Print details of a Car and a Bicycle instance
💡 Why This Matters
🌍 Real World
Inheritance helps organize code when building apps with related objects like vehicles, animals, or UI elements.
💼 Career
Understanding class-only inheritance is important for Swift developers to design clean, reusable, and maintainable code.
Progress0 / 4 steps
1
Create the base class Vehicle
Create a class called Vehicle with two properties: speed of type Int and capacity of type Int. Initialize both properties in an initializer.
Swift
Hint
Use class keyword to define a class. Add properties and an initializer method.
2
Create subclass Car inheriting from Vehicle
Create a class called Car that inherits from Vehicle. Add a property brand of type String. Create an initializer that takes speed, capacity, and brand and calls the superclass initializer for speed and capacity.
Swift
Hint
Use class Car: Vehicle to inherit. Call super.init to initialize the base class.
3
Create subclass Bicycle inheriting from Vehicle
Create a class called Bicycle that inherits from Vehicle. Add a property hasBasket of type Bool. Create an initializer that takes speed, capacity, and hasBasket and calls the superclass initializer for speed and capacity.
Swift
Hint
Similar to Car, inherit from Vehicle and call super.init.
4
Create instances and print details
Create an instance of Car named myCar with speed 120, capacity 5, and brand "Toyota". Create an instance of Bicycle named myBike with speed 25, capacity 1, and hasBasket true. Print the details of both using print statements.
Swift
Hint
Create instances with the init method and use print with string interpolation to show properties.
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