Today we cover Inheritance, but in a strict, interview-realistic way:
❌ No abstraction for abstraction’s sake ✅ Inheritance only when the problem forces it ✅ Heavy implementation with clear rules 🟢 Day 4 — Inheritance & Method Overriding (Machine Coding Style)
🎯 Day 4 Objective
By the end of Day 4, the candidate should be able to:
Implement inheritance when required Share common data and behavior using a base class Override methods correctly Handle different rules for different subclasses Pass machine-coding tests involving parent–child classes Today is about reusing structure, not logic guessing.
1️⃣ Teaching Script — What to Say
Start with This Reality
“Most of the time, you do NOT need inheritance.
But when the problem says ‘There are multiple types of X’, you almost always do.”
Examples from interviews:
Employee → FullTime / PartTime Account → Savings / Current Inheritance exists to share common structure, not to look smart.
2️⃣ Core Idea of Inheritance (Only What’s Needed)
A child class IS A parent class, but with extra or different behavior.
3️⃣ Small Code to Understand (Minimal, Clear)
Example — Employee Base Class
class Employee:
def __init__(self, emp_id: int, salary: int):
self.emp_id = emp_id
self.salary = salary
def calculate_pay(self) -> int:
return self.salary
Child Class
class PartTimeEmployee(Employee):
def calculate_pay(self) -> int:
return self.salary // 2
Key Observations
Caller does NOT need to know type 🧠 Machine coding tests whether you override correctly, not creatively.
4️⃣ Mandatory Warm-up Practice
Task
Implement the following:
Base class: Shape
- area() → int
Child classes:
- Square (side)
- Rectangle (length, width)
Rules:
👉 This ensures candidate understands override mechanics.
5️⃣ Machine Coding Rules (Day 4 Rules)
Use inheritance only where specified Do NOT duplicate common fields Call super().__init__() when required Override only the required methods Keep logic exactly as specified 6️⃣ Main Problem 1 — Account Types (Classic Interview Question)
Problem Statement
You are implementing a banking system with different account types.
All accounts have:
But withdrawal rules differ.
Class Definitions (DO NOT MODIFY)
class Account:
def __init__(self, account_number: str, balance: int):
pass
def withdraw(self, amount: int) -> bool:
pass
def get_balance(self) -> int:
pass
class SavingsAccount(Account):
def withdraw(self, amount: int) -> bool:
pass
class CurrentAccount(Account):
def withdraw(self, amount: int) -> bool:
pass
Requirements (Very Strict)
Account (Base Class)
Stores account_number and balance get_balance() returns balance withdraw() in base class should: (Base class should not allow withdrawal) SavingsAccount
Cannot withdraw more than balance If amount <= 0 → return False If amount > balance → return False CurrentAccount
Can overdraft up to -1000 If amount <= 0 → return False If balance - amount < -1000 → return False Example
sa = SavingsAccount("S1", 500)
sa.withdraw(600) # False
sa.withdraw(200) # True
ca = CurrentAccount("C1", 500)
ca.withdraw(1200) # True (balance becomes -700)
ca.withdraw(500) # False (would exceed overdraft)
7️⃣ Main Problem 2 — Notification System
Problem Statement
You are implementing a notification system.
Different notification types behave differently, but share a common interface.
Class Definitions (DO NOT MODIFY)
class Notification:
def send(self, message: str) -> bool:
pass