OOP Part 2 — Inheritance & Polymorphism
10. OOP Part 2 — Inheritance & Polymorphism
10.1Reusing Code (Inheritance)
Think of a general Vehicle blueprint vs an ElectricCar — an ElectricCar inherits all general vehicle features (wheels, steering) and adds battery features.
SHOW:
WHAT HAPPENED HERE
Car(Vehicle) inherited the start() method directly from parent class Vehicle without rewriting any code.THE TECHNICAL NAME
TAKEAWAY
class Child(Parent): to inherit existing methods.10.2Extending & Overriding Methods (super())
Think of upgrading a phone app — the update keeps the core functionality of the previous version but adds custom new buttons.
SHOW:
WHAT HAPPENED HERE
super().__init__(name) called the parent Person constructor to set up self.name, then added self.student_id.THE TECHNICAL NAME
TAKEAWAY
super().__init__() to reuse parent initialization logic.10.3Polymorphism (One Interface, Many Forms)
Think of a universal TV remote "Play" button — pressing it on a DVD player or streaming app starts playback in its own unique way.
SHOW:
WHAT HAPPENED HERE
Dog and Cat have a speak() method. We called animal.speak() in a loop without needing to know which specific animal class it was!THE TECHNICAL NAME
TAKEAWAY
10.4Encapsulation Basics (Internal Data Privacy)
Think of a car dashboard — drivers press the gas pedal without directly touching or messing with fuel injection lines inside the engine.
SHOW:
WHAT HAPPENED HERE
_balance with an underscore to signal that it's internal. We provided a get_balance() method to safely read it.THE TECHNICAL NAME
_ prefix signals a protected attribute.TAKEAWAY
_ and getter methods to encapsulate internal data.GOTCHA 1
Forgetting to call super().__init__() in a child class constructor, leaving inherited properties uninitialized.
GOTCHA 2
Overusing deep inheritance chains (more than 2-3 levels) making code hard to trace.
GOTCHA 3
Assuming _protected attributes in Python are hard-locked private (Python relies on developer convention).
WHERE THIS SHOWS UP IN REAL CODE
GUI frameworks and game engines build hierarchies (e.g., Button inherits from Widget) and use polymorphism to render UI components.
TRY THIS YOURSELF
Create class Animal with speak() method and subclass Dog overriding speak() returning 'Woof'.
Notice how Manager overrides get_pay() while reusing Employee's constructor via super().
Demonstrate class inheritance, super() initializer calls, method overriding, and polymorphism.
Stretch Challenge: Explain Inheritance & Polymorphism with Employee hierarchy.
Steps:
- Print explanation:
Inheritance reuses parent code; polymorphism allows uniform method calls.. Workerwithwork(self)returningWorking....Developer(Worker)overridingwork(self)returningWriting Code....- Loop
[Worker(), Developer()]printing output.
What is inheritance in Object-Oriented Programming?
What is the purpose of the `super()` function in a child class initializer?
What is method overriding?
What is polymorphism?
What naming convention in Python signals that an attribute is intended as protected/private?
What syntax defines a child class `Dog` inheriting from parent `Animal`?
What happens if a child class does NOT define an `__init__` method?
What is Encapsulation?
In Duck Typing ('If it walks like a duck...'), what matters most in Python?
# comments · oop-advanced
- @priya · 2h ago
The typewriter hero is such a nice touch.
- @alex · 1h ago
Totally agree! Made it super engaging.
- @sam · 1d ago
Finally a course where I can run code without leaving the page.