Object-Oriented Programming in Swift
Course Title: Swift Programming: From Basics to Advanced Development Section Title: Object-Oriented Programming (OOP) in Swift Topic: Inheritance, polymorphism, and encapsulation
Introduction
In the previous topic, we covered the basics of classes and structures in Swift. In this topic, we will dive deeper into the fundamental principles of object-oriented programming (OOP) in Swift: inheritance, polymorphism, and encapsulation. These concepts will help you create robust, reusable, and maintainable code.
Inheritance
Inheritance is a mechanism in OOP that allows one class to inherit properties and methods from another class. The class that is being inherited from is called the superclass or parent class, while the class that inherits is called the subclass or child class.
Here is an example of inheritance in Swift:
// Superclass (Parent)
class Vehicle {
var brand: String
var model: String
init(brand: String, model: String) {
self.brand = brand
self.model = model
}
func honk() {
print("Honk!")
}
}
// Subclass (Child)
class Car: Vehicle {
var numberOfDoors: Int
init(brand: String, model: String, numberOfDoors: Int) {
self.numberOfDoors = numberOfDoors
super.init(brand: brand, model: model)
}
func lockDoors() {
print("Doors locked!")
}
}
In this example, the Car
class inherits the brand
, model
, and honk()
method from the Vehicle
class. The Car
class also has its own property numberOfDoors
and method lockDoors()
.
Polymorphism
Polymorphism is the ability of an object to take on multiple forms. In Swift, we can achieve polymorphism through method overriding and method overloading.
Method overriding is when a subclass provides a different implementation of a method that is already defined in its superclass.
// Superclass (Parent)
class Animal {
func sound() {
print("Generic animal sound")
}
}
// Subclass (Child)
class Dog: Animal {
override func sound() {
print("Woof!")
}
}
// Usage
let dog = Dog()
dog.sound() // Output: Woof!
In this example, the Dog
class overrides the sound()
method of the Animal
class.
Method overloading is when multiple methods with the same name can be defined, but with different parameter lists.
class Calculator {
func calculate(num1: Int, num2: Int) -> Int {
return num1 + num2
}
func calculate(num1: Double, num2: Double) -> Double {
return num1 + num2
}
}
// Usage
let calculator = Calculator()
print(calculator.calculate(num1: 10, num2: 20)) // Output: 30
print(calculator.calculate(num1: 10.5, num2: 20.7)) // Output: 31.2
In this example, the Calculator
class has two methods with the same name calculate
, but with different parameter lists.
Encapsulation
Encapsulation is the concept of bundling data and its methods that operate on that data into a single unit. In Swift, we can achieve encapsulation through classes and structures.
class BankAccount {
private var balance: Double
init(balance: Double) {
self.balance = balance
}
func deposit(amount: Double) {
balance += amount
}
func getBalance() -> Double {
return balance
}
}
// Usage
let account = BankAccount(balance: 1000.0)
account.deposit(amount: 500.0)
print(account.getBalance()) // Output: 1500.0
In this example, the BankAccount
class encapsulates the balance
data and provides methods deposit
and getBalance
to access and modify the data.
Conclusion
In this topic, we covered inheritance, polymorphism, and encapsulation, which are fundamental principles of object-oriented programming in Swift. Understanding these concepts will help you create robust, reusable, and maintainable code.
Example Use Cases
Inheritance: Create a class hierarchy for different types of employees, such as
Manager
,Developer
, andDesigner
, where each class inherits properties and methods from a baseEmployee
class.Polymorphism: Create a game with different types of characters, such as
Warrior
,Mage
, andArcher
, where each character has its own implementation of aattack
method.Encapsulation: Create a class to represent a real-world object, such as a
Car
, where the class encapsulates the object's data and provides methods to access and modify the data.
External Resources
- Apple Developer Documentation: Inheritance
- Apple Developer Documentation: Polymorphism
- Apple Developer Documentation: Encapsulation
What's Next?
In the next topic, we will cover understanding access control and visibility in Swift.
Images

Comments