Activity 1
Design a small hierarchy
Create a LearningItem superclass and ArticleLesson subclass. Override summary to include a reading duration.
Hint: Keep only genuinely shared state and behavior in the superclass.
Swift Fundamentals
Understand class identity, encapsulation, inheritance, overriding, and when object-oriented design fits Swift.
Before you begin
Learning stage
Object-oriented programming groups mutable state and behavior inside objects with identity. Swift classes are reference types, so multiple variables can refer to the same instance and observe the same mutations.
A class can inherit properties and methods from a superclass. A subclass specializes behavior with override, while final prevents further inheritance or overriding. Encapsulation limits which implementation details callers can change directly.
Learning stage
The subclass inherits shared state and replaces one behavior while remaining usable as its superclass type.
class LearningItem {
let title: String
init(title: String) {
self.title = title
}
func summary() -> String {
title
}
}
final class VideoLesson: LearningItem {
let duration: Int
init(title: String, duration: Int) {
self.duration = duration
super.init(title: title)
}
override func summary() -> String {
"\(title) — \(duration) min video"
}
}
let lesson = VideoLesson(title: "OOP in Swift", duration: 16)
print(lesson.summary())Learning stage
These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.
Learning stage
Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.
Learning stage
Work through these activities in a small Xcode project. No embedded playground is required on this site.
Activity 1
Create a LearningItem superclass and ArticleLesson subclass. Override summary to include a reading duration.
Hint: Keep only genuinely shared state and behavior in the superclass.
Activity 2
Assign one class instance to two variables, mutate it through one variable, and predict what the other variable observes.
Hint: Both variables refer to the same instance.
Learning stage
Answer each question before revealing the explanation.
QUESTION 1
A reference to the same instance is copied, not an independent object value.
QUESTION 2
It prevents the class from being subclassed; on a member, it prevents overriding.
Learning stage
Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.
Short answer: Swift classes provide identity, reference semantics, encapsulation, inheritance, overriding, and runtime polymorphism.
Classes combine state and behavior in instances with identity. Access control supports encapsulation, subclasses inherit and specialize behavior, and a superclass reference can dispatch to an overridden implementation. Swift also supports value and protocol-oriented designs, so classes should be chosen when identity or framework requirements justify them.
Possible follow-ups
Learning stage
OOP is one tool in Swift, not the default answer to every model. Use classes when identity, shared lifetime, inheritance, or a framework contract is meaningful.