ALL MODULESmodule_10.py

OOP Part 2 — Inheritance & Polymorphism

~60 minTheoryLive CodingChallengeQuiz
theory.md

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:

PYTHON
WHAT HAPPENED HERE
Car(Vehicle) inherited the start() method directly from parent class Vehicle without rewriting any code.
THE TECHNICAL NAME
Passing features from a parent class (superclass) to a child class (subclass) is inheritance.
TAKEAWAY
Create subclasses using 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:

PYTHON
WHAT HAPPENED HERE
super().__init__(name) called the parent Person constructor to set up self.name, then added self.student_id.
THE TECHNICAL NAME
super() delegates calls to parent classes. Replacing a parent method with a child version is method overriding.
TAKEAWAY
Use 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:

PYTHON
WHAT HAPPENED HERE
Both 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
Treating different classes through the exact same method signature is polymorphism.
TAKEAWAY
Polymorphism lets different classes share the same method name with custom behaviors.

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:

PYTHON
WHAT HAPPENED HERE
We prefixed _balance with an underscore to signal that it's internal. We provided a get_balance() method to safely read it.
THE TECHNICAL NAME
Bundling data and restricting direct outside access is encapsulation. The _ prefix signals a protected attribute.
TAKEAWAY
Use a leading underscore _ 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'.

live_coding — Employee Hierarchy & Salary Processor

Notice how Manager overrides get_pay() while reusing Employee's constructor via super().

Python not loaded yet.
demo — Demonstrate class inheritance, super() initializer calls, method overriding, and polymorphism.

Demonstrate class inheritance, super() initializer calls, method overriding, and polymorphism.

challenge_1

Warm-up 1: Basic subclass creation.

Steps:

  1. Class Animal with method sound(self) printing Generic sound.
  2. Subclass Dog(Animal).
  3. Instantiate d = Dog() and call d.sound().
Python not loaded yet.
challenge_2

Warm-up 2: Override a method in a subclass.

Steps:

  1. Class Shape with describe(self) returning Shape.
  2. Subclass Square(Shape) overriding describe(self) returning Square.
  3. Instantiate s = Square() and print s.describe().
Python not loaded yet.
challenge_3

Warm-up 3: Use super() to initialize parent properties.

Steps:

  1. Class Device(brand).
  2. Subclass Phone(Device, model) calling super().__init__(brand).
  3. Instantiate p = Phone("Apple", "iPhone") and print Apple iPhone.
Python not loaded yet.
challenge_4

Apply-it 1: Polymorphism with payment processors.

Steps:

  1. Classes CreditCard and PayPal, both with method pay(amount).
  2. CreditCard.pay(100) prints Card charged: $100.
  3. PayPal.pay(100) prints PayPal charged: $100.
  4. Loop through [CreditCard(), PayPal()] calling pay(100).
Python not loaded yet.
challenge_5

Apply-it 2: Encapsulation with protected attribute and getter.

Steps:

  1. Class BankAccount with protected _balance.
  2. Method get_balance(self) returning _balance.
  3. Instantiate with 250 and print Balance: $250.
Python not loaded yet.
challenge_6

Apply-it 3: Multi-level inheritance (Vehicle -> FourWheeler -> SUV).

Steps:

  1. Vehicle with type = "Vehicle".
  2. FourWheeler(Vehicle) setting wheels = 4.
  3. Instantiate suv = FourWheeler() and print Vehicle has 4 wheels.
Python not loaded yet.
challenge_7

Stretch Challenge: Explain Inheritance & Polymorphism with Employee hierarchy.

Steps:

  1. Print explanation: Inheritance reuses parent code; polymorphism allows uniform method calls..
  2. Worker with work(self) returning Working....
  3. Developer(Worker) overriding work(self) returning Writing Code....
  4. Loop [Worker(), Developer()] printing output.
Python not loaded yet.
quiz — 9 questions
30s question timers are paused. Start individually or all together.
# question 1

What is inheritance in Object-Oriented Programming?

# question 2

What is the purpose of the `super()` function in a child class initializer?

# question 3

What is method overriding?

# question 4

What is polymorphism?

# question 5

What naming convention in Python signals that an attribute is intended as protected/private?

# question 6

What syntax defines a child class `Dog` inheriting from parent `Animal`?

# question 7

What happens if a child class does NOT define an `__init__` method?

# question 8

What is Encapsulation?

# question 9

In Duck Typing ('If it walks like a duck...'), what matters most in Python?

discussion

# 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.

# further_reading