📖 Scenario: You are building a simple app to manage different types of vehicles. Each vehicle can start and stop, but some vehicles have special features like flying or floating. You want to decide whether to use Protocol-Oriented Programming (POP) or Object-Oriented Programming (OOP) to organize your code.
🎯 Goal: Learn how to create vehicle types using both POP and OOP patterns in Swift. You will define protocols and classes, then decide which approach fits best for adding new vehicle behaviors.
📋 What You'll Learn
Create a protocol called Vehicle with start() and stop() methods
Create a class called Car that conforms to Vehicle and implements the methods
Create a protocol called Flyable with a fly() method
Create a struct called FlyingCar that conforms to Vehicle and Flyable
Print outputs showing how Car and FlyingCar behave
💡 Why This Matters
🌍 Real World
Many apps need to model real-world objects with shared and unique behaviors. Choosing between OOP and POP helps organize code for easier maintenance and extension.
💼 Career
Understanding POP and OOP decision patterns is important for Swift developers to write clean, reusable, and scalable code in iOS and macOS app development.
Progress0 / 4 steps
1
Define the Vehicle protocol
Create a protocol called Vehicle with two methods: start() and stop(). Both methods should not take any parameters and return nothing.
Swift
Hint
Protocols define a blueprint of methods. Use protocol Vehicle {} and add the two method signatures inside.
2
Create the Car class conforming to Vehicle
Create a class called Car that conforms to the Vehicle protocol. Implement the start() method to print "Car started" and the stop() method to print "Car stopped".
Swift
Hint
Use class Car: Vehicle and implement the two methods with print statements.
3
Define Flyable protocol and FlyingCar struct
Create a protocol called Flyable with a method fly() that returns nothing. Then create a struct called FlyingCar that conforms to both Vehicle and Flyable. Implement start() to print "FlyingCar started", stop() to print "FlyingCar stopped", and fly() to print "FlyingCar is flying".
Swift
Hint
Protocols can be combined. Use struct FlyingCar: Vehicle, Flyable and implement all required methods with print statements.
4
Create instances and print behaviors
Create an instance called myCar of type Car and an instance called myFlyingCar of type FlyingCar. Call start() and stop() on myCar. Call start(), fly(), and stop() on myFlyingCar. Print the outputs.
Swift
Hint
Create instances with let and call the methods in order. The output should show the correct messages.
Practice
(1/5)
1. Which statement best describes the main difference between Protocol-Oriented Programming (POP) and Object-Oriented Programming (OOP) in Swift?
easy
A. POP focuses on protocols and structs, while OOP focuses on classes and inheritance.
B. POP uses only classes, and OOP uses only structs.
C. POP does not support code reuse, but OOP does.
D. OOP uses protocols, but POP does not.
Solution
Step 1: Understand POP and OOP basics
POP uses protocols to define behavior and structs or classes to implement it. OOP uses classes and inheritance to share and override behavior.
Step 2: Compare the main focus of each paradigm
POP emphasizes protocols and value types (structs), while OOP emphasizes classes and inheritance.
Final Answer:
POP focuses on protocols and structs, while OOP focuses on classes and inheritance. -> Option A
D. protocol Vehicle { func drive() }; class Car: Vehicle { func drive() { print("Driving") } }
Solution
Step 1: Check protocol and struct syntax
protocol Vehicle { func drive() }; struct Car: Vehicle { func drive() { print("Driving") } } correctly declares a protocol with a method and a struct conforming by implementing that method.
Step 2: Identify syntax errors in other options
class Vehicle { func drive() }; struct Car inherits Vehicle { func drive() { print("Driving") } } uses invalid inheritance syntax for struct. protocol Vehicle { func drive() }; struct Car: Vehicle { var speed: Int = 0 } misses method requirement. protocol Vehicle { func drive() }; class Car: Vehicle { func drive() { print("Driving") } } uses class instead of struct.
Hint: Protocols declare methods; structs implement them with correct syntax [OK]
Common Mistakes:
Using 'inherits' keyword with structs
Not implementing all protocol methods
Confusing class and struct declarations
3. What will be the output of the following Swift code?
protocol Greetable {
func greet() -> String
}
class Person: Greetable {
func greet() -> String {
"Hello from Person"
}
}
class Employee: Person {
override func greet() -> String {
"Hello from Employee"
}
}
let p: Greetable = Person()
let e: Greetable = Employee()
print(p.greet())
print(e.greet())
medium
A. Hello from Person
Hello from Employee
B. Hello from Person
Hello from Person
C. Hello from Employee
Hello from Employee
D. Compilation error due to protocol conformance
Solution
Step 1: Understand protocol conformance and inheritance
Person conforms to Greetable with greet() returning "Hello from Person". Employee inherits Person and overrides greet() to return "Hello from Employee".
Step 2: Analyze output of print statements
Variable p is Person instance, so p.greet() returns "Hello from Person". Variable e is Employee instance, so e.greet() returns "Hello from Employee".
Final Answer:
Hello from Person
Hello from Employee -> Option A
Quick Check:
Inheritance override works, prints Person then Employee [OK]
Hint: Subclass overrides method; output reflects actual instance type [OK]
Common Mistakes:
Assuming protocol type erases override
Confusing struct and class inheritance
Expecting compilation error for override
4. Identify the error in this Swift code that tries to use POP and OOP together:
A. Circle struct must use 'override' keyword for draw().
B. Protocol Drawable is missing required methods.
C. Class Shape cannot conform to protocol Drawable.
D. Struct cannot inherit from a class; Circle cannot inherit from Shape.
Solution
Step 1: Check inheritance rules for structs and classes
In Swift, structs cannot inherit from classes. Circle is a struct trying to inherit from class Shape, which is invalid.
Step 2: Verify protocol conformance and method implementation
Shape correctly conforms to Drawable. Circle tries to override draw(), but inheritance is invalid for structs.
Final Answer:
Struct cannot inherit from a class; Circle cannot inherit from Shape. -> Option D
Quick Check:
Structs cannot inherit classes in Swift [OK]
Hint: Structs cannot inherit classes; use protocols for shared behavior [OK]
Common Mistakes:
Thinking structs can inherit classes
Confusing protocol conformance with inheritance
Using 'override' in struct methods
5. You want to design a Swift app where multiple types share behavior but also have unique data. You want flexibility to add new behaviors without deep inheritance chains. Which approach fits best?
hard
A. Use deep class inheritance with base classes and subclasses for all types.
B. Use Protocol-Oriented Programming with protocols and structs to share behavior and data.
C. Use only classes without protocols to simplify code.
D. Use global functions instead of protocols or classes.
Solution
Step 1: Analyze design goals
The goal is to share behavior, have unique data, and avoid deep inheritance chains for flexibility.
Step 2: Match goals to programming paradigms
POP uses protocols and structs to share behavior flexibly without inheritance. OOP with deep inheritance is less flexible and can be complex.
Final Answer:
Use Protocol-Oriented Programming with protocols and structs to share behavior and data. -> Option B
Quick Check:
Flexible behavior sharing = POP with protocols & structs [OK]
Hint: For flexible shared behavior, prefer POP with protocols and structs [OK]
Common Mistakes:
Choosing deep inheritance causing rigid design
Ignoring protocols for behavior sharing
Using global functions losing type safety
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.