0
0
Pythonprogramming~30 mins

Extending parent behavior in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Extending Parent Behavior
📖 Scenario: Imagine you are creating a simple system to manage different types of employees in a company. Each employee has a name and a method to describe their work. You want to create a special type of employee called a Manager who does everything a normal employee does but also manages a team.
🎯 Goal: You will build two classes: Employee and Manager. The Manager class will extend the Employee class and add extra behavior by extending the parent method.
📋 What You'll Learn
Create a class called Employee with an __init__ method that takes name as a parameter.
Add a method called work in Employee that returns the string "Employee {name} is working".
Create a class called Manager that inherits from Employee.
Override the work method in Manager to extend the behavior of Employee.work by adding " and managing the team" to the returned string.
Create an instance of Manager with the name "Alice" and print the result of calling its work method.
💡 Why This Matters
🌍 Real World
Extending parent behavior is common when you want to add or change features in a new type of object without rewriting existing code. For example, managers have all employee features plus extra responsibilities.
💼 Career
Understanding inheritance and method overriding is essential for writing clean, reusable code in many programming jobs, especially in software development and object-oriented design.
Progress0 / 4 steps
1
Create the Employee class
Create a class called Employee with an __init__ method that takes a parameter name and stores it in self.name. Also, add a method called work that returns the string f"Employee {self.name} is working".
Python
Need a hint?

Remember to use self.name = name inside the __init__ method to save the name.

2
Create the Manager class inheriting Employee
Create a class called Manager that inherits from Employee. Do not add any methods yet.
Python
Need a hint?

Use parentheses to inherit: class Manager(Employee):

3
Override the work method in Manager
In the Manager class, override the work method. Inside it, call the parent class work method using super().work() and add the string " and managing the team" to the result. Return the combined string.
Python
Need a hint?

Use super().work() to get the parent method's result.

4
Create Manager instance and print work result
Create an instance of Manager called manager with the name "Alice". Then print the result of calling manager.work().
Python
Need a hint?

Create the instance with Manager("Alice") and print manager.work().