Chapter 77: Swift OOP

1. What is OOP? (the clearest possible explanation)

OOP = Object-Oriented Programming is a way of organizing code by grouping data and behavior together into objects.

Instead of writing one giant list of instructions (procedural style), you create small, self-contained things (objects) that know their own data and know how to do things with that data.

Real-life analogy everyone understands:

  • Think of a smartphone as an object.
    • Data (properties): screen brightness, battery level, model name, owner
    • Behavior (methods): takePhoto(), sendMessage(), chargeBattery(), setBrightness(80)

You don’t need to know how the inside works — you just tell the phone what to do.

OOP lets you model your program the same way: create “smart objects” that manage their own state and behavior.

Swift supports OOP very well, but it also encourages value types (structs + enums) and protocol-oriented programming (POP). Still, understanding classic OOP is extremely important — many real APIs, UIKit, SwiftUI patterns, and third-party libraries are built around it.

2. The Four Pillars of OOP — explained in Swift

Pillar 1 – Encapsulation

Encapsulation = “hide the internal details, expose only what is necessary”

In Swift this is done mainly with:

  • private / fileprivate / internal / public
  • computed properties
  • methods that control access
Swift

Real-life rule:

Make data private Expose behavior (methods) that safely change or read the data

Pillar 2 – Abstraction

Abstraction = “show only the essential features, hide the complexity”

In Swift:

  • Use protocols to define what something can do (not how)
  • Use classes / structs to hide implementation details

Classic example — shapes:

Swift

→ The caller doesn’t care whether it’s a circle or rectangle — it only cares that it can draw() and has area.

Pillar 3 – Inheritance

Inheritance = a class can inherit properties & methods from another class (parent → child)

Swift supports single inheritance only (one direct parent).

Swift

When to use inheritance (modern Swift view):

  • Yes: clear “is-a” relationship (Dog is an Animal)
  • Yes: you want to share common behavior & state
  • No: just to reuse code — prefer composition (has-a) or protocols

Pillar 4 – Polymorphism

Polymorphism = “many forms” — same method name, different behavior depending on the actual object type.

This happens automatically thanks to inheritance + protocols.

Swift

Protocol-based polymorphism (modern Swift favorite):

Swift

5. Real-life examples — OOP patterns you will actually use

Example 1 – UIViewController hierarchy (UIKit classic)

Swift

Example 2 – Payment processor (very common backend / fintech)

Swift

6. Quick Summary – The Four Pillars in Swift

Pillar Swift implementation Real-life feeling
Encapsulation private, fileprivate, computed properties Hide internal state, expose safe methods
Abstraction Protocols, protocol extensions Define “what” not “how”
Inheritance class Child: Parent “is-a” relationship (Dog is an Animal)
Polymorphism method overriding + protocol conformance Same message → different behavior

7. Small Practice – Try these

  1. Create a base class Vehicle with makeSound() Create two subclasses: Car (“Vroom!”) and Bike (“Vruum-vruum!”) Put them in an array [Vehicle] and call makeSound() on each
  2. Create a protocol Payable with method processPayment(amount:) Implement it for two payment methods (Razorpay, Paytm)
  3. Create a class Employee with name, salary (private) Add method giveRaise(percent:) that safely increases salary

Paste your code here if you want feedback or want to see more elegant versions!

What would you like to explore next?

  • Protocol-oriented programming (POP) vs classic OOP
  • Struct vs Class — when to choose which
  • Inheritance vs composition (modern preference)
  • Access control (private, fileprivate, internal, public, open)
  • OOP in SwiftUI (ObservableObject, @StateObject…)
  • Or move to another topic (optionals, arrays, closures, switch…)

Just tell me — we’ll continue in the same clear, detailed, patient style 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *