Chapter 78: OOP Intro

1. What does “Object-Oriented Programming” actually mean? (the clearest possible explanation)

OOP is a way of thinking about and organizing code by grouping:

  • data (properties / variables)
  • behavior (methods / functions)

together inside objects.

Instead of writing one long list of instructions (like old procedural programming), you create small, smart, self-contained things (objects) that:

  • know their own data
  • know how to do things with that data
  • can talk to other objects

Real-life analogy (the one that clicks for almost everyone):

Think of a smartphone as an object.

  • Data it remembers: screen brightness, battery level, phone number, owner name, photos, apps installed…
  • Behavior it can do: makeCall(), takePhoto(), sendMessage(), chargeBattery(), setBrightness(80), playMusic(…)

When you use the phone, you don’t open the back cover and manually move electrons. You just tell the phone what you want (“take a photo”, “call mom”).

OOP lets you model your program the same way:

  • Create a User object that knows its name, email, profile picture, and can login(), logout(), updateProfile()
  • Create a ShoppingCart object that knows its items, total price, and can addItem(), removeItem(), applyCoupon(), checkout()

You don’t manipulate the data directly — you ask the object to do things for you.

That’s the core idea.

2. The Four Classic Pillars of OOP (explained in Swift)

Almost every book and course talks about these four ideas. Here’s what they really mean in Swift, with clear examples.

Pillar 1 – Encapsulation

“Hide the internal details — only expose what is safe and necessary”

In Swift this is mainly done with:

  • private / fileprivate / internal / public / open access modifiers
  • computed properties (get/set)
  • methods that control how data is read or changed
Swift

Key lesson: Make data private, expose behavior (methods) that safely change or read the data.

Pillar 2 – Abstraction

“Show only the essential features — hide the complicated details”

In Swift, abstraction is mostly done with protocols.

A protocol says: “This thing can do these things” — without caring how it does them.

Swift

Key lesson: The caller doesn’t need to know whether it’s a circle or rectangle — it only needs to know it can draw() and has area.

Pillar 3 – Inheritance

“A child class can inherit properties & methods from a parent class”

Swift supports single inheritance (only one direct parent).

Swift

Modern Swift note: Inheritance is used much less in modern Swift compared to 10 years ago. Developers now prefer protocol conformance + composition (has-a) over deep inheritance trees.

Pillar 4 – Polymorphism

“Many forms” — same method name, different behavior depending on the actual object

This happens automatically thanks to inheritance + protocols.

Swift

Protocol-based polymorphism (modern favorite):

Swift

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

Example 1 – UIViewController inheritance (UIKit classic)

Swift

Example 2 – Payment processor (very common in fintech / e-commerce)

Swift

6. Quick Summary — The Four Pillars in Swift

Pillar Main Swift tools 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 property speed and method move() Create two subclasses: Car and Bike that override move() with different messages
  2. Create a protocol Payable with method processPayment(amount:) Implement it for two payment gateways (Razorpay, Paytm)
  3. Create a class Employee with private salary Add public 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 *