Build a Notification System
Advanced · Write the code · from Python Inheritance: Subclasses, Polymorphism, and super()
From the lesson: Python Inheritance: Subclasses, Polymorphism, and super()
Think about how scientists classify animals. A Dog is a type of Mammal, which is a type of Animal. Each level inherits traits from the one above it: all animals breathe, all mammals are warm-blooded, and all dogs bark.
Read the lessonBuild a notification system:
1. Notification base class with __init__(self, message) and a send(self) method returning 'Sending: <message>'
2. EmailNotification(Notification) with __init__(self, message, email). Override send() to return 'Email to <email>: <message>'
3. SMSNotification(Notification) with __init__(self, message, phone). Override send() to return 'SMS to <phone>: <message>'
Create one of each:
EmailNotification('Hello!', 'alice@test.com')SMSNotification('Hi there!', '555-1234')Loop through both and print send() for each.
More Inheritance exercises
- Create a Vehicle SubclassBeginner
- Override the describe() MethodBeginner
- Predict the super() OutputIntermediate
- Fix the Inheritance BugIntermediate
- Build a Shape Area CalculatorIntermediate
All Python exercises · 50 practice problems · Object-Oriented Programming quiz