Inheritance in Practice
Build a real project combining everything from Week 4.
Introduction
Let's put everything together. This lesson builds a complete example that uses inheritance, child classes, method overriding, and super() -- all in one project.
Project: Employee Management System
We will build a system with a base Employee class and two specialized child classes: Manager and Developer.
class Employee:
def __init__(self, name, emp_id, salary):
self.name = name
self.emp_id = emp_id
self.salary = salary
def get_info(self):
return f"{self.name} (ID: {self.emp_id}) - ${self.salary:,.2f}"
class Manager(Employee):
def __init__(self, name, emp_id, salary, department):
super().__init__(name, emp_id, salary)
self.department = department
self.team = []
def add_to_team(self, employee):
self.team.append(employee)
def get_info(self):
base = super().get_info()
return f"{base}\n Department: {self.department}, Team: {len(self.team)}"
class Developer(Employee):
def __init__(self, name, emp_id, salary, language):
super().__init__(name, emp_id, salary)
self.language = language
def get_info(self):
base = super().get_info()
return f"{base}\n Language: {self.language}"
Using the System
# Create employees
dev1 = Developer("Alice", "D001", 85000, "Python")
dev2 = Developer("Bob", "D002", 90000, "JavaScript")
mgr = Manager("Carol", "M001", 110000, "Engineering")
# Build the team
mgr.add_to_team(dev1)
mgr.add_to_team(dev2)
# Display info for everyone
staff = [dev1, dev2, mgr]
for person in staff:
print(person.get_info())
print()
# Alice (ID: D001) - $85,000.00
# Language: Python
#
# Bob (ID: D002) - $90,000.00
# Language: JavaScript
#
# Carol (ID: M001) - $110,000.00
# Department: Engineering, Team: 2
Notice how each object's get_info() produces different output, even though we loop through them the same way. This is the power of inheritance and overriding working together.
Second Example: Vehicle Hierarchy
class Vehicle:
def __init__(self, brand, year):
self.brand = brand
self.year = year
def describe(self):
return f"{self.year} {self.brand}"
class Car(Vehicle):
def __init__(self, brand, year, doors):
super().__init__(brand, year)
self.doors = doors
def describe(self):
base = super().describe()
return f"{base} ({self.doors}-door)"
class ElectricCar(Car):
def __init__(self, brand, year, doors, range_km):
super().__init__(brand, year, doors)
self.range_km = range_km
def describe(self):
base = super().describe()
return f"{base}, Range: {self.range_km}km"
e = ElectricCar("Tesla", 2024, 4, 500)
print(e.describe())
# 2024 Tesla (4-door), Range: 500km
This is multilevel inheritance in action. Each level adds its own details, and super() chains them all together seamlessly.
Week 4 Recap
- Inheritance: Reuse code from a parent class in a child class.
- Child Classes: Add new attributes and methods on top of what they inherit.
- Method Overriding: Replace a parent's method with the child's own version.
- super(): Call the parent's methods to extend rather than replace behavior.
- Types: Single, multilevel, hierarchical, and multiple inheritance.
What's Next?
Next week: Polymorphism -- where different objects respond to the same method in different ways. You have already seen a preview of this with method overriding. Week 5 takes it much further.
Best Practices
- Plan your class hierarchy before writing code -- sketch out what is shared and what is unique.
- Keep the base class general and each child specific to its role.
- Use
super()consistently to avoid code duplication across the hierarchy.
Common Mistakes
- Putting too much logic in the base class -- keep it focused on what all children share.
- Forgetting to call
super().__init__()in multilevel chains -- every level needs it. - Not testing each class independently before using them together.
Practice Exercises
- Extend the Employee system: add an
Internclass with auniversityattribute and overrideget_info(). - Build a
Schoolsystem: create aPersonbase withStudentandTeacherchildren. Each has a uniquerole()method. - Create a three-level hierarchy of your own choice. Use
super()at every level and demonstrate method overriding.
Summary
- Real projects combine inheritance, overriding, and
super()to build clean hierarchies. - A well-designed base class makes it easy to add new child classes later.
- Multilevel inheritance chains build increasingly specialized classes.
- Week 4 covered the full foundation of inheritance -- next up is polymorphism.