Method inheritance
Inheritance is powerful because it allows us to reuse and customize code without rewriting existing code. By calling methods of the parent class within the child class, we reuse all the code in those methods, making our code concise and manageable.
In this exercise, you'll continue working with the Manager
class that is inherited from the Employee
class. You'll add new data to the class, and customize the give_raise()
method from Chapter 1 to increase the manager's raise amount by a bonus percentage whenever they are given a raise.
A simplified version of the Employee
class, as well as the beginning of the Manager
class from the previous lesson is provided for you in the script pane.
This exercise is part of the course
Object-Oriented Programming in Python
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
class Employee:
def __init__(self, name, salary=30000):
self.name = name
self.salary = salary
def give_raise(self, amount):
self.salary += amount
class Manager(Employee):
# Add a constructor
def __init__(self, name, ____, ____):
# Call the parent's constructor
____.____(____, ____, salary)
# Assign project attribute
____
def display(self):
print("Manager ", self.name)