Python Inheritance, Access Modifiers & Method Overriding
Organize object hierarchies through inheritance. Master encapsulation with access modifiers (public, protected, private), extend base classes with extends, invoke parent methods with parent::, and prevent extension using the final keyword.
"Access modifiers are like security clearances in a corporate headquarters: public is the open lobby, protected is the employee-only workspace shared with sister branches (subclasses), and private is the CEO personal locked vault."
Deep Dive: How It Works
Access Modifiers: public (everywhere), protected (class & subclasses only), private (declaring class only).
Inheritance: class Manager extends Employee inherits all non-private methods and properties.
Parent Invocation: parent::__construct(...) or parent::doWork() calls overridden base implementations.
Final Classes & Methods: final prevents other classes from extending or overriding critical security routines.
Syntax Blueprint
<?php
class BaseAccount {
protected float $balance = 0;
}
class SavingsAccount extends BaseAccount {
public function addInterest() { $this->balance *= 1.05; }
}Use protected for shared subclass state and extends to inherit base class behavior.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Trying to access private properties from a subclass ($this->privateField).Why it happens: private properties are strictly restricted to the declaring class.
How to fix: Change modifier to protected if subclasses legitimately need access.
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.
Extend Base Notification Class
Create a class Notification with public function send(string $to): string returning "Notification sent to {$to}".
Create a subclass EmailNotification extends Notification that overrides send(string $to): string to return "Email dispatched to {$to}".
In main script, create $email = new EmailNotification() and print the result of $email->send("admin@zen.com").
Finished reading and practicing?
Mark this lesson as completed to update your course progress.