Inheritance lets one thing copy and extend another. In Swift, only classes can do this because they work with shared, changeable data.
When you want one object to reuse and add to another object's behavior.
When you need to create a family of related objects with shared features.
When you want to change or extend how an object works without rewriting it.
When you want to use polymorphism to treat different objects the same way.
When you want to manage shared, changeable data safely.
Syntax
Swift
class ParentClass {
// properties and methods
}
class ChildClass: ParentClass {
// extra properties and methods
}
Only class types can inherit from another class.
Structures and enums cannot use inheritance in Swift.
Examples
A Dog class inherits from Animal and changes the sound() method.
Swift
class Animal {
func sound() {
print("Some sound")
}
}
class Dog: Animal {
overridefunc sound() {
print("Bark")
}
}
Structures cannot inherit from other structures.
Swift
struct Point {
var x: Intvar y: Int
}
// This is not allowed:// struct ColoredPoint: Point {// var color: String// }
Sample Program
This program shows a Car class inheriting from Vehicle and changing the description.
Swift
class Vehicle {
func description() {
print("I am a vehicle")
}
}
class Car: Vehicle {
overridefunc description() {
print("I am a car")
}
}
let myCar = Car()
myCar.description()
OutputSuccess
Important Notes
Classes use references, so inheritance works with shared, changeable data.
Structs and enums are value types and copy data, so inheritance doesn't fit their design.
Swift uses protocols to share behavior for structs and enums instead of inheritance.
Summary
Inheritance is only for classes because they share and change data by reference.
Structs and enums use copying, so they don't support inheritance.
Use protocols to share behavior with structs and enums.
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