Create a subclass
The purpose of child classes, or sub-classes, is to customize and extend the functionality of the parent class.
Recall the Employee
class from earlier in the course. In most organizations, managers have more privileges and responsibilities than regular employees. 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.
In this exercise, you'll create a Manager
child class and, later in the course, you'll add specific functionality to the class.
This exercise is part of the course
Introduction to Object-Oriented Programming in Python
Exercise instructions
- Add a
Manager
class that inherits fromEmployee
. - Use a keyword to leave the
Manager
class empty. - Create an object called
mng
using theManager
class, setting the name to"Debbie Lashko"
and salary to86500
. - Print the name attribute of
mng
.
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
____
# Add a keyword to leave this class empty
____
# Define a Manager object
mng = ____
# Print mng's name
print(____)