Create a subclass
The purpose of child classes -- or sub-classes, as they are usually called - is to customize and extend functionality of the parent class.
Recall the Employee
class from earlier in the course. In most organizations, managers enjoy more privileges and more responsibilities than a regular employee. So it would make sense to introduce a Manager
class that has more functionality than Employee
.
But a Manager
is still an employee, so the Manager
class should be inherited from the Employee
class.
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:
MIN_SALARY = 30000
def __init__(self, name, salary=MIN_SALARY):
self.name = name
if salary >= Employee.MIN_SALARY:
self.salary = salary
else:
self.salary = Employee.MIN_SALARY
def give_raise(self, amount):
self.salary += amount
# Define a new class Manager inheriting from Employee
____
# Define a Manager object
mng = ____
# Print mng's name
____