In this tutorial, we’ll build a simple Python application for managing employment details. We’ll create classes for employees, managers, and departments, and explain each step with comments for beginners to understand.
1. Employee Class
class Employee:
"""
The Employee class represents individual employees in the organization.
"""
def __init__(self, id, name, salary):
"""
Constructor to initialize employee attributes.
"""
self.id = id # Unique identifier for each employee
self.name = name # Name of the employee
self.salary = salary # Salary of the employee
def display_details(self):
"""
Method to display employee details.
"""
print("Employee ID:", self.id)
print("Name:", self.name)
print("Salary:", self.salary)
Explanation:
- The
Employeeclass represents individual employees in the organization. - It contains attributes such as
id,name, andsalaryto store employee information. - The
__init__method is a constructor that initializes these attributes when a new employee object is created. - The
display_detailsmethod prints the employee’s ID, name, and salary to the console.
2. Manager Class
class Manager(Employee):
"""
The Manager class represents managers in the organization.
Inherits from the Employee class.
"""
def __init__(self, id, name, salary, department):
"""
Constructor to initialize manager attributes.
"""
super().__init__(id, name, salary)
self.department = department # Department managed by the manager
def display_details(self):
"""
Method to display manager details.
"""
super().display_details()
print("Department:", self.department)
Explanation:
- The
Managerclass represents managers in the organization and inherits from theEmployeeclass. - It adds an additional attribute
departmentto store the department managed by the manager. - The
__init__method initializes all attributes, including those inherited from theEmployeeclass. - The
display_detailsmethod overrides the method in the superclass to include the department information when displaying manager details.
3. Department Class
class Department:
"""
The Department class represents departments in the organization.
"""
def __init__(self, name):
"""
Constructor to initialize department attributes.
"""
self.name = name # Name of the department
self.employees = [] # List to store employees in the department
def add_employee(self, employee):
"""
Method to add an employee to the department.
"""
self.employees.append(employee)
def display_details(self):
"""
Method to display department details.
"""
print("Department Name:", self.name)
print("Employees:")
for emp in self.employees:
emp.display_details()
Explanation:
- The
Departmentclass represents departments in the organization. - It contains attributes such as
nameto store the department name and a listemployeesto store employee objects in the department. - The
__init__method initializes the department name and creates an empty list of employees. - The
add_employeemethod adds an employee to the department’s list of employees. - The
display_detailsmethod prints the department name and the details of all employees in the department.
Written By
Welcome to DevTechTutor.com, your ultimate resource for mastering web development and technology! Whether you're a beginner eager to dive into coding or an experienced developer looking to sharpen your skills, DevTechTutor.com is here to guide you every step of the way. Our mission is to make learning web development accessible, engaging, and effective.