Activity 1
Build a polymorphic collection
Create ArticleItem and VideoItem subclasses, store both as LessonItem values, and call an overridden summary on each.
Hint: The array's element type can be the common superclass.
Swift Fundamentals
Work safely with superclass references, overridden behavior, runtime type checks, and conditional casts.
Before you begin
Learning stage
Polymorphism lets code work through a shared superclass or protocol while each concrete value provides its own behavior. When an overridden method is called through a superclass reference, Swift dispatches to the runtime instance's implementation.
Type casting inspects or refines runtime type information. is checks whether a value has a type, as performs a cast known to succeed, as? produces an optional for a conditional cast, and as! traps if the forced cast fails.
Learning stage
A mixed superclass collection uses polymorphic summaries and conditionally accesses video-specific data.
class LessonItem {
let title: String
init(title: String) { self.title = title }
func summary() -> String { title }
}
final class VideoItem: LessonItem {
let duration: Int
init(title: String, duration: Int) {
self.duration = duration
super.init(title: title)
}
override func summary() -> String { "Video: \(title)" }
}
let items: [LessonItem] = [
LessonItem(title: "Swift Basics"),
VideoItem(title: "Polymorphism", duration: 14)
]
for item in items {
print(item.summary())
if let video = item as? VideoItem {
print("Duration: \(video.duration) min")
}
}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 ArticleItem and VideoItem subclasses, store both as LessonItem values, and call an overridden summary on each.
Hint: The array's element type can be the common superclass.
Activity 2
Use as? to access one property that exists only on VideoItem and handle the non-video case without force casting.
Hint: Optional binding combines the cast check and unwrapping.
Learning stage
Answer each question before revealing the explanation.
QUESTION 1
nil
A conditional cast produces an optional of the requested type.
QUESTION 2
The implementation belonging to the instance's runtime type.
Learning stage
Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.
Short answer: It lets code use a shared type while runtime instances provide specialized behavior.
A superclass or protocol reference defines the operations callers can use. Concrete subclasses or conforming types implement those operations, and dispatch selects the appropriate implementation. This reduces caller knowledge of concrete types and enables substitution.
Possible follow-ups
Learning stage
Polymorphism lets callers depend on shared behavior while concrete types remain specialized. Cast only at boundaries where more specific runtime knowledge is genuinely required.