Skip to content
Prepare to Grow

Swift Fundamentals

Object-Oriented Programming in Swift

Understand class identity, encapsulation, inheritance, overriding, and when object-oriented design fits Swift.

Beginner4 min read2 guided activities

Before you begin

What you will understand

  • Explain object identity and reference semantics.
  • Encapsulate class state and behavior.
  • Create subclasses and override inherited behavior.
  • Use final when inheritance should stop.
01

Learning stage

Concept Explanation

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.

  • Identity distinguishes instances even when their property values match.
  • Encapsulation exposes a controlled interface around internal state.
  • Inheritance specializes an existing class relationship.
  • override and final make customization rules explicit.
02

Learning stage

Syntax & Examples

The subclass inherits shared state and replaces one behavior while remaining usable as its superclass type.

LearningItem.swiftswift
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())
Expected output: “OOP in Swift — 16 min video”.
03

Learning stage

Real-World Usage

These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.

  • UIKit view controllers and many Apple framework APIs use class identity and inheritance.
  • Long-lived coordinators and shared services may require reference identity.
  • Subclassing is appropriate only when the subtype truly is a specialized form of its superclass.
04

Learning stage

Common Mistakes

Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.

  • Using inheritance only to reuse code when composition would model the relationship better.
  • Exposing mutable internal state that callers can place into invalid combinations.
  • Forgetting that two variables can refer to the same class instance.
  • Creating deep inheritance hierarchies that make behavior difficult to locate.
05

Learning stage

Practice Exercises

Work through these activities in a small Xcode project. No embedded playground is required on this site.

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.

Activity 2

Observe reference semantics

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.

06

Learning stage

Knowledge Check

Answer each question before revealing the explanation.

QUESTION 1

What is copied when one class variable is assigned to another?

Reveal answer

A reference to the same instance is copied, not an independent object value.

QUESTION 2

What does final prevent on a class?

Reveal answer

It prevents the class from being subclassed; on a member, it prevents overriding.

07

Learning stage

Interview Preparation

Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.

How does Swift support object-oriented programming?

Short answer: Swift classes provide identity, reference semantics, encapsulation, inheritance, overriding, and runtime polymorphism.

Explore the detailed answer

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

  • • When would you prefer a struct?
  • • What is the purpose of final?
08

Learning stage

Key Takeaways

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.

  • Classes have reference semantics and identity.
  • Encapsulation controls access to mutable state.
  • Inheritance expresses specialization.
  • Composition and value types are often simpler than deep class hierarchies.