Python Classes, Reference Semantics & Inheritance
When you need shared mutable state or single-inheritance hierarchies, use Classes. Master reference semantics (multiple variables pointing to the same instance), custom initializers, subclassing with extends, super.init calls, and deinitializers (deinit).
"A class instance is like a Google Doc: when you share the link with 5 team members, everyone edits the exact same live document. Changes made by any member are instantly visible to everyone."
Deep Dive: How It Works
Reference Semantics: Assigning a class instance to a new variable creates another reference to the exact same object in memory.
Identity Operator (===): Checks whether two variables point to the exact same memory address (unlike == which checks value equality).
Inheritance: Subclasses inherit methods and properties from a superclass (class Manager: Employee).
Deinitializer (deinit): Executes automatically when an instance reference count drops to zero and memory is deallocated.
Syntax Blueprint
class Animal {
func speak() { print("...") }
}
class Dog: Animal {
override func speak() { print("Woof!") }
}Inherit with : BaseClass and override methods with the override keyword.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Forgetting the override keyword when redefining a superclass method.Why it happens: Swift compiler requires explicit override to prevent accidental method shadowing.
How to fix: Add the override keyword before func.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Create an Account Class Hierarchy
Create a class Account with var balance: Double and an init(balance: Double).
Create a subclass PremiumAccount: Account with a method addBonus(amount: Double) that adds amount to balance.
In top-level code, create let acc = PremiumAccount(balance: 100.0), call acc.addBonus(amount: 25.0), and print "Balance: $\(acc.balance)".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.