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