Swift / Object-Oriented Programming in Swift
Understanding Inheritance and Polymorphism
In this tutorial, you'll learn about the principles of inheritance and polymorphism in Swift. You'll understand how these concepts enhance flexibility and reusability of your code.
Section overview
5 resourcesCovers OOP concepts like classes, structures, and protocols in Swift.
1. Introduction
Goal
In this tutorial, we aim to provide you with a comprehensive understanding of inheritance and polymorphism in Swift. These are fundamental concepts in object-oriented programming (OOP) that not only allow you to write more efficient code but also promote the reusability of your code.
Learning Outcomes
By the end of this tutorial, you will be able to:
- Understand the concepts of inheritance and polymorphism.
- Apply inheritance to create subclasses and superclasses.
- Use polymorphism to design flexible and reusable code.
- Implement these concepts in Swift programming.
Prerequisites
To get the most out of this tutorial, you should have a basic understanding of Swift programming and its syntax. Familiarity with object-oriented programming concepts will be beneficial but not required.
2. Step-by-Step Guide
Inheritance
Inheritance is a fundamental concept in OOP where a class can inherit properties and behaviors from another class, often referred to as the parent class or superclass.
class Animal {
var name: String
func speak() { }
init(name: String) {
self.name = name
}
}
class Dog: Animal {
override func speak() {
print("Woof!")
}
}
In this example, Dog is a subclass of Animal and it inherits the name property and the speak() method from Animal. The override keyword is used to provide a new implementation of a method that is already defined in the superclass.
Polymorphism
Polymorphism is another fundamental concept in OOP that allows one type to appear as and be used like another type. In Swift, there are two types of polymorphism: subtype polymorphism and parametric polymorphism.
let dog: Animal = Dog(name: "Rex")
dog.speak() // prints "Woof!"
In this example, although dog is declared as type Animal, it's actually an instance of Dog. When we call dog.speak(), it prints "Woof!", which shows subtype polymorphism in action.
3. Code Examples
Inheritance
Here's an example of a Car class that has two subclasses, ElectricCar and GasCar.
class Car {
var make: String
var model: String
init(make: String, model: String) {
self.make = make
self.model = model
}
func drive() {
print("Driving a \(make) \(model).")
}
}
class ElectricCar: Car {
override func drive() {
print("Driving a \(make) \(model), it's electric!")
}
}
class GasCar: Car {
override func drive() {
print("Driving a \(make) \(model), it's gas-powered!")
}
}
let tesla = ElectricCar(make: "Tesla", model: "Model S")
tesla.drive() // prints "Driving a Tesla Model S, it's electric!"
let ford = GasCar(make: "Ford", model: "F-150")
ford.drive() // prints "Driving a Ford F-150, it's gas-powered!"
4. Summary
We've covered:
- The concepts of inheritance and polymorphism in Swift.
- How to create subclasses and superclasses using inheritance.
- How to use polymorphism to design flexible and reusable code.
To continue your learning journey, practice these concepts with more complex examples. You can also learn about other OOP concepts like encapsulation and abstraction. Check out the Swift documentation for more information.
5. Practice Exercises
-
Create a
Birdclass with afly()method. Then createEagleandPenguinsubclasses that override thefly()method. Eagles can fly but penguins cannot. -
Implement polymorphism with a
Shapeclass and its subclassesCircleandRectangle. Each shape should have anareamethod that calculates its area.
Try to solve these on your own before looking at the solutions below.
Solutions
- Bird classes:
class Bird {
var name: String
func fly() { }
init(name: String) {
self.name = name
}
}
class Eagle: Bird {
override func fly() {
print("\(name) is flying high!")
}
}
class Penguin: Bird {
override func fly() {
print("\(name) can't fly.")
}
}
- Shape classes:
class Shape {
func area() -> Double {
return 0
}
}
class Circle: Shape {
var radius: Double
init(radius: Double) {
self.radius = radius
}
override func area() -> Double {
return Double.pi * radius * radius
}
}
class Rectangle: Shape {
var width: Double
var height: Double
init(width: Double, height: Double) {
self.width = width
self.height = height
}
override func area() -> Double {
return width * height
}
}
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article