In this example, we are going to create a very basic template of a government employee payment system that would be used to pay three different kinds of workers. The classes we would consider in this example are mainstream government workers, university workers and oil workers.
The first class to be created would be that of the mainstream workers because it contains all the attributes common to all the three classes of workers.
Next, we create the university class of workers which would inherit all the attributes of the mainstream workers and in addition would have the attribute ‘allowance’ which is peculiar to only this class of workers.
Finally, we would create the oil worker class which would also inherit all attributes from the mainstream worker class but would have the peculiar attribute ‘offshore’ attributed to this class of workers only.
Below is the code to execute the Employee Payment Platform System (EPPS).
print('Employee Payment Platform System (EPPS)')
print('------------------------------------------')
class Employee:
def __init__(self, name, age, rank):
self.name = name
self.age = age
self.rank = rank
def pay(self):
print('pay')
class University(Employee):
def allowance(self):
print('allowance')
class Oil(Employee):
def offshore(self):
print('offshore')
print('')
employee1 = Employee('Mike', 34,
'Manager')
employee1.pay()
print('')
print('--------------------')
university1 = University('Ali',
28, 'Dean')
university1.pay()
university1.allowance()
print('')
print('--------------------')
oil1 = Oil('Jay', 45, 'Supervisor')
oil1.pay()
oil1.offshore()
When we
run the code we have the following output:
Employee Payment
Platform System (EPPS)
------------------------------------------
pay
--------------------
pay
allowance
--------------------
pay
offshore
From the code above, all employees have
common parameters (name, age, rank) and a common method (function) ‘pay’.
However, the university class inherits
all those attributes from the employee class and in addition have the peculiar
method, ‘allowance’ attributed to this class only.
Similarly, the oil worker class inherits
all the attributes from the employee class and in addition have the peculiar attribute
‘offshore’.
We can then add our employees based on
the class they belong and they would automatically get all the payment they are
entitled to.
If required, at any time, we can add,
remove, modify any of the attributes in any of the classes and the changes
would affect only the targeted employees.